0

I'm developing a mobile web, which contents come from a foreign XML, and i'm having trouble with tags. They're coming with style attributes, which I think would be easy to erase using preg_replace in php before showing the contents. The problem comes when a img tag is found within a text... something like: "Hel<img .../>lo My name is Alfred<br/>". If I just erase the style attribute (generally coming with display:float), the image breaks the text, making it horrible to read.

My solution is: using preg_replace, I "clean" all image tags, BUT then I need to take those tags and place them after the next <br/>, </p>, etc. (every final of paragraph tag). I think it will at least make the page more readable and organized.

The problem: don't know how to get every img tag's index, just after I cleaned it, and then find the next end of paragraph to place it there.

Example-->

before:

Hell<img .../>o my name is Alfred.<br/>
<p>I come <img .../>from England</p>

after:

Hello my name is Alfred<br/>
<img .../>
<p>I come from England</p>
<img .../>

Thanks in advance.

EDIT ---

My doubt is: if I found an img tag (<img />) in text (maybe using preg_replace, because I first needed to find a img tag, verify its attributes and change them if necessary), how do I get the index inside the whole string (by whole string I mean the whole html document read as a string) so I could take the whole tag and move it to the next end of paragraph?

3
  • Use (.+?) in place of ... and you got yourself a regex. With a litte effort it's possible to find something in the many duplicates such as php - Extracting from between two strings using regex, albeit many regex answers have been botched lately. Commented Oct 14, 2011 at 15:19
  • Hi, thanks for response, but with dots I didn't mean a regular expression. I was trying to say the image tag could have anything there (which actually doesn't really matter for my doubt). My doubt is: if I found an img tag (<img />) in text, how do I get the index inside the whole string so I could take the whole tag and move it to the next end of paragraph? Commented Oct 15, 2011 at 14:22
  • Are you replacing one paragraph at a time or the whole file? Commented Oct 15, 2011 at 14:36

1 Answer 1

0

You won't get the position of the match from any preg_ function. On the other hand you can do the replacement with preg_replace_callback and pile up the matches to print them afterwards. Example:

function noimages( $match )
{
  $stack = array();
  $img_regex = '%<img.*(/>|</img>)%ixU';
  $noimages = preg_replace_callback($img_regex, 
    function ($imgtag) use(&$stack) { array_push($stack, $imgtag[0]); return ''; },
    $match);
  return array($noimages,$stack);
}

So for example:

$match = '<p>I'm not <img src="yyy.jpg"/> interested on this <img src="zzz.jpg"></img> issue </p>';
list($withoutimg, $imgs) = noimages( $match );

Will return the block in $withoutimg and an array with the two img tags in $imgs.

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

Comments

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.