PDA

View Full Version : <img tag stripping strpos str_replace substr


svincent2000
10-17-2002, 10:19 PM
$body = "some words some words some words some words <img src=\file.name string > some more words<img src=\file.name string >";

// need to remove img tags from the total body text

do {
$i = $i + 1;
$startpos = strpos($body, "<img", $startpos);
$endpos = strpos($body, ">", $startpos);
$length = ($endpos + 1) - $startpos;
$final = substr ($body, $startpos, $length);
$startpos = $endpos;
$body = str_replace ($final, "", $body);
}
while ((strpos("<img", $startpos) != false));


I am trying to strip out HTML image tags that start "<img" and end ">" (like they do). If I have additional HTML tags (like "<p>") the do...while statement stops - why? How can I get round this? I thought I'd scripted it so that it ONLY looks for and removes <img ... >

my alternative code is:



$startpos = 0;
$i = 0;
$count_img_tag = substr_count($body, "<img");


for ($i = 1; $count_img_tag; $i++)
{
$startpos = strpos($body, "<img", $startpos);
$endpos = strpos($body, ">", $startpos);
$length = ($endpos + 1) - $startpos;
$final = substr ($body, $startpos, $length);
$startpos = $endpos;
$body = str_replace ($final, "", $body);
}


Any help and suggestions please...

Simon

Spookster
10-17-2002, 10:28 PM
I don't have time to write out some code for this at the moment but the logic is simple. I've written so many parsing programs the logic becomes second nature.


search the string for: "<im"

go back to that first "<" and remove everything until next ">" is encountered.

Of course might also want to account for some other variations that might occur such as:

< img .....>
<IMG ...>
< IMG ...>

You can use regular expressions to match the patterns.

svincent2000
10-18-2002, 10:40 AM
Spookster,

Can you show me the code if poss that would be great

Thanks

svincent2000
10-18-2002, 01:18 PM
It's ok I've fixed it

Thanks

Simon