Jump to content

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}}

I think it's the correct way around but here's the reverse anyway

 

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

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.