Jump to content

why can't i user the regex twice?


ianco

Recommended Posts

Hi all,

 

So, I have this code stored in a database:

 

<figure><img src="http://example.com/Cat.bmp" alt="text" /><figcaption>text</figcaption></figure>

 

and i use regex php to turn it into the following in a text editor:

 

$regex = "#{{([^|]*)\|\|([^|]*)\|\|([^|]*)}}#";
$replace = '<figure><img src="$1" alt="$2" /><figcaption>$3</figcaption></figure><p>';
$content = preg_replace($regex, $replace, $content);

 

This works fine except when I want to insert two figures. So if i have two figures separated by a line the regex changes it to

 

{{http://variouspictures.tk/wp-content/uploads/2011/07/Cat.bmp" alt="text" />text

 

Can anyone explain to me why this happens and suggest a solution. Thanks

 

Ian

Link to comment
https://forums.phpfreaks.com/topic/251690-why-cant-i-user-the-regex-twice/
Share on other sites

Sorry my first post doesn't make too much sense

 

I'm entering (or starting with)

<figure><img src="http://example.com/Cat.bmp" alt="text" /><figcaption>text</figcaption></figure>

 

and I want it to turn it into

 

{{http://variouspictures.tk/wp-content/uploads/2011/07/Cat.bmp||text||text}}

 

this works fine when there is only one figure but when I have two figures i.e.,

<figure><img src="http://example.com/Cat.bmp" alt="text" /><figcaption>text</figcaption></figure>
<figure><img src="http://example.com/Cat.bmp" alt="text" /><figcaption>text</figcaption></figure>

 

i only get

{{http://variouspictures.tk/wp-content/uploads/2011/07/Cat.bmp" alt="text" />text

 

instead of

{{http://variouspictures.tk/wp-content/uploads/2011/07/Cat.bmp||text||text}}{{http://variouspictures.tk/wp-content/uploads/2011/07/Cat.bmp||text||text}}

No, I mean that regex you provided will change:

{{http://variouspictures.tk/wp-content/uploads/2011/07/Cat.bmp||text||text}}

into

<figure><img src="http://variouspictures.tk/wp-content/uploads/2011/07/Cat.bmp" alt="text" /><figcaption>text</figcaption></figure>

 

 

 

Where is the regex to change:

<figure><img src="http://variouspictures.tk/wp-content/uploads/2011/07/Cat.bmp" alt="text" /><figcaption>text</figcaption></figure>

into

{{http://variouspictures.tk/wp-content/uploads/2011/07/Cat.bmp||text||text}}

 

That is the desired question right?

 

Where is the regex to change:

<figure><img src="http://variouspictures.tk/wp-content/uploads/2011/07/Cat.bmp" alt="text" /><figcaption>text</figcaption></figure>

into

{{http://variouspictures.tk/wp-content/uploads/2011/07/Cat.bmp||text||text}}

 

That is the desired question right?

 

this regex is:

$regex = '#<figure><img src="([^\']*)" alt="([^\']*)" /><figcaption>([^\']*)</figcaption></figure>#';
$replace = '{{$1||$2||$3}}';
$pagecontent = preg_replace($regex, $replace, $pagecontent);

 

except when I have two figures it cocks up.  :shrug:

Change:

$regex = '#<figure><img src="([^\']*)" alt="([^\']*)" /><figcaption>([^\']*)</figcaption></figure>#';

into

$regex = '#<figure><img src="([^\']*?)" alt="([^\']*?)" /><figcaption>([^\']*?)</figcaption></figure>#';

 

Your * quantifier was being greedy and matching everything it could. It needed the ? after it to tell it to lookout for the next literal character that needs to be matched, that being the closing speech mark.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.