Hello Everyone.

Can you please advise how can I replace a string from this "[" to "]" this character and replace it with blank string? I believe I will use str_replace but I am having a hard time doing this.

The quick brown fox jumped over [caption id="attachment_223" align="alignleft" width="900"] the head of the lazy dog.

Thanks in advance.

Recommended Answers

All 2 Replies

You'll need a regex for that. Something like this:

$string = 'The quick brown fox jumped over [caption id="attachment_223" align="alignleft" width="900"] the head of the lazy dog.';
$newstring = preg_replace('/\[.*?\]/i', '', $string);
echo $newstring;

Thanks for the reply pritaeas. It worked. I need to start working on my regex.

Thank you again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.