0

i need to replace string with array value and add some text after and before array value. i tried to find solution in google and stackoverflow.but i failed. Then I have a string, for example:

$string = "Hello...! :emo01:";

I have an array with Keys and Values, example:

$arr = array(":emo01:"=>"1f325", ":emo02:"=>"1f326", ":emo03:"=>"1f5b1");

and i need to code this function

function my_Function($arr,$string){

}

Would give the return as "Hello...! <img src='http://www.domain.com/1f325.png' />". how to write this 'my_Function' function.

2
  • See: stackoverflow.com/q/8163746/3933332 Commented Aug 13, 2016 at 13:20
  • Have you even looked at the link, basically duplicate, above?! Commented Aug 13, 2016 at 13:50

1 Answer 1

1

Solution work on general case.

$string = "Hello...! :emo01:";
$string2 = "Hello...! :emo01::emo02:";

$arr = array(":emo01:"=>"1f325", ":emo02:"=>"1f326", ":emo03:"=>"1f5b1");


function my_Function($arr, $string){

    $arrNewArray = [];

    foreach($arr as $key => $value ) {
        $arrNewArray[$key] = '<img src="http://yourdomain.com/'. $value .'.png" />';
    }

    return str_replace(array_keys($arrNewArray), $arrNewArray, $string);

}

echo my_Function($arr, $string);
echo my_Function($arr, $string2);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.