0

I have the following str_replace code which takes a given comment string and replaces any instances with a dot followed by [++] with the word He with a capital letter. Otherwise it replaces with he with no capital letters.

$comment = str_replace(array(". [++]","[++]"), array(". He","he"), $comment);

Although this works in most cases. It doesnt work if my comment string has [++] tag in the beginning of the sentence or if the tag is after a line break or two. It only works if the dot is next to the tag.

Any ideas how I can get it to replace with the capital He if its after a line break or the first tag in beginning of the comment string?

3
  • 5
    Turn to the dark side Luke, learn regular expressions Commented Jan 4, 2017 at 13:33
  • Might need to use regular expressions.... Commented Jan 4, 2017 at 13:36
  • I don't see it, you need to show a string that doesn't work. Commented Jan 4, 2017 at 13:41

1 Answer 1

1

Try the preg_replace() function (http://php.net/manual/en/function.preg-replace.php):

$comment = preg_replace(
    array("~^\[\+\+\]~", "~(\.\s+)\[\+\+\]~", "~\[\+\+\]~"),
    array("He", "$1He", "he"),
    $comment
);
Sign up to request clarification or add additional context in comments.

2 Comments

thanks. But it's return a blank result. Seems to be emptying out the entire $comment string.
@Ahmed, I made an error in the second pattern. Now I've fixed it.

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.