0

I want to replace the

 </color='#any hexadecimal color code '> 

to

</color>

I have tried following but nothing happened

$bio_info="some string </color='#any hexadecimal color code'>";    
preg_replace("/</color/", "</color>", $bio_info);
1
  • 1
    Nothing happened? You should have gotten an error complaining about unrecognized modifiers in the regexp, because you forgot to escape the /. Commented Jun 1, 2018 at 1:22

2 Answers 2

1

Your regexp doesn't include the ='#any hexadecimal color code ' part of the tag that you want to replace. You also forgot to escape the /. And you need to assign the result to a variable if you want to use it.

$bio_info="some string </color='#any hexadecimal color code'>";  
$result = preg_replace('#</color=[^>]*>#', '</color>', $bio_info);

I've used a different regexp delimiter so I don't need to escape the /. [^>]* will match any sequence of characters other than >, so this matches the rest of the tag.

Sign up to request clarification or add additional context in comments.

Comments

0

Simply fill with any char between opening and closing tag :

$bio_info="some string </color='#any hexadecimal color code'>";    
preg_replace("/<\/color.*>/", "</color>", $bio_info);

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.