Search code examples
phpstringstr-replace

Get the first and second character from string using PHP


Please read this sentence..

Hello my name is ##John##
This is my ##question##
Thank you for ##reading## and giving your ##Answer##

See there are many ## characters right? Now What I want is to make the words inside the ## character bold which means to insert the words inside the HTML <strong></strong> tag, So what can I do to do this using PHP? What I have tried is.....

$converted = str_replace('##', '<strong>', $toconvert);

But the problem is the second tag don't have closing / at the front, means all are tag only, and if sometimes the string have many ######## in it, then this will make problem too,

This exactly what I want

 Hello my name is <strong>John</strong>
 This is my <strong>question</strong>
 Thank you for <strong>reading</strong> and giving your 
    <strong>Answer</strong>

So please help me to achieve this.

Thank you.

Edited

Keep in mind all the string is come from Database and it is stored in one variable not in array, I just make it in new line to make it clear for you but I it is stored in PHP variable.

Thank you once again.


Solution

  • use regular expression for that

    preg_replace('/##(.*?)##/', '<strong>$1</strong>', $input_lines);