user3134994
user3134994

Reputation: 25

How to remove [img] bbcode with php

I have a string

$content = "your image is [img]url to image.png[/img] now you can use it";

With php script I want

$content = "your image is now you can use it";

Upvotes: 0

Views: 543

Answers (2)

newfurniturey
newfurniturey

Reputation: 38436

If there is a single-instance of [img][/img], you can use a combination of substr() and strpos():

$first = substr($content, 0, strpos($content, '[img]'));
$end = substr($content, strpos($content, '[/img]') + 6);
$content = $first . $end;

If there can be multiple instances within the same string, you'll need to put it in a loop:

$openImg = strpos($content, '[img]');
while ($openImg !== false) {
    $first = substr($content, 0, $openImg);
    $end = substr($content, strpos($content, '[/img]') + 6);
    $content = $first . $end;

    $openImg = strpos($content, '[img]');
}

Upvotes: 1

Amal Murali
Amal Murali

Reputation: 76646

$content = "your image is [img]url to image.png[/img] now you can use it";
echo preg_replace("/\[img\](.+?)\[\/img\]/i", '', $content);

Output:

your image is  now you can use it

Upvotes: 2

Related Questions