1

How can I replace a string from a particular string? Please see my example below.

$string_value="<b>Man one</b> <img src=\"http://www.abc.com/image1.jpg\">and <b>Man two</b> <img src=\"http://www.abc.com/img2.png\">";

Now my expected out put is = <b>Man one</b> and <b>man two</b>. only the image tag should be deleted.

So I have to cut the full string from "<img" to ">" from string $string_value. So how can I cut the full string between "<img" to ">" using preg_replace or anything else.

The replacing parameter should be blank space.

3 Answers 3

2

Looks like you want to strip the tags.
You can do it easily by calling the function strip_tags which gets HTML and PHP tags stripped from a given string.

$string_value = strip_tags($string_value);

EDIT:

Since you want to strip only the <img tag, you can make use of this function:

function strip_only($str, $tags) {
        if(!is_array($tags)) {
                $tags = (strpos($str, '>') !== false ? explode('>', str_replace('<', '', $tags)) : array($tags));
                if(end($tags) == '') array_pop($tags);
        }
        foreach($tags as $tag) $str = preg_replace('#</?'.$tag.'[^>]*>#is', '', $str);
        return $str;
}

and call it as:

$string_value = strip_only($string_value,'img');

Working link

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

3 Comments

Thanks ,it works.But i need to delete the img tag only.Because i have many other html tags that i don't mentioned on my example.so ,is their any way to delete the ima tag only that i mention??pls reply
string strip_tags ( string $str [, string $allowable_tags ] ) You can allow what tags you want.
again thx bro,but i have many other html tags on that string.Basically i need to replace the img tag only.so pls help to remove the img tag only...thx again
1

You can use regular expressions to exclude IMG tags:

<?php
$text = "Man one <img src=\"http://www.abc.com/image1.jpg\">and Man two <img src=\"http://www.abc.com/img2.png\">";
$pattern = "/<img(.*?)>/i";
$replace = '';
print preg_replace($pattern,$replace,$text);
?>

Comments

1

Just use strip_tags($string_value); and you will get your desired output.

3 Comments

Thanks ,it works.But i need to delete the img tag only.Because i have many other html tags that i don't mentioned on my example.so ,is their any way to delete the ima tag only that i mention??pls reply
@riad: strip_tags takes an additional parameter that specifies allowable tags, just pass a string containing all the tags that you want to allow.
@Dear kiselovs, thx. i get this.their have many other html tags.so its defficult to include all the tags.I need to replace the img tag only.so pls help me to remove the image tag only..thx again..riad

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.