Joey Morani
Joey Morani

Reputation: 26581

How to remove a custom tag and content between in PHP?

Say if I had this tag in a string:
[phpbay]{keyword}, 30, "", "", "", "", "", "", "", "", "", "", "", "", "2", "", "", ""[/phpbay]
Could I remove it and the content between the tags? Maybe using regex? Thanks!

Upvotes: 0

Views: 1632

Answers (5)

Anon2011
Anon2011

Reputation: 11

I personally would use str_replace or str_ireplace (case Insensitive)

$Find = array("[phpbay]","[/phpbay]");
$Replace = array("<b>","</b>");
$Source = "[phpbay]Hello World[/phpbay]";


//Couldn't get any simpler...Replace [phpbay]Content[/phpbay] with <b>Content</b>

echo str_ireplace($Find,$Replace,$Source);

Tried and tested, it works.

Hope this helps!

Upvotes: -1

RiaD
RiaD

Reputation: 47619

preg_replace('~\[phpbay\](.+?)\[/phpbay\]~','',$string);

Upvotes: 1

mario
mario

Reputation: 145482

Yes, a regex might be feasible here:

$str = preg_replace('#\[phpbay][{\w},\s\d"]+\[/phpbay]#', "", $str);

This just removes the "tag" from the string if it contains only such characters as in your example. If you only wanted to remove it if it contains e.g. your example 30 or a specific {keyword} you would have to make the regex more specific.

See also https://stackoverflow.com/questions/89718/is-there-anything-like-regexbuddy-in-the-open-source-world for some tools that might help.

Upvotes: 1

Joseph Silber
Joseph Silber

Reputation: 219920

This regEx will give you everything between those two tags:

\[phpbay](.+)\[/phpbay]

Upvotes: 1

Marc B
Marc B

Reputation: 360592

$string = "[phpbay]blah blah blah [/phpbay]";
$stripped_string = preg_replace('~\[\/?phpbay\]~', '', $string);

if I'm reading your question right, and want only the "blah blah blah" part to remain.

Upvotes: 1

Related Questions