ianco Posted November 23, 2011 Share Posted November 23, 2011 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 Quote Link to comment Share on other sites More sharing options...
requinix Posted November 23, 2011 Share Posted November 23, 2011 That regex inserts a if it matches. There is no in the bad output you posted, therefore the regex didn't match. What text are you entering? Quote Link to comment Share on other sites More sharing options...
ianco Posted November 23, 2011 Author Share Posted November 23, 2011 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}} Quote Link to comment Share on other sites More sharing options...
joe92 Posted November 23, 2011 Share Posted November 23, 2011 That regex you provided in your first post looks like the one to change it back to the <figure>etc.? Quote Link to comment Share on other sites More sharing options...
ianco Posted November 23, 2011 Author Share Posted November 23, 2011 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); Quote Link to comment Share on other sites More sharing options...
joe92 Posted November 23, 2011 Share Posted November 23, 2011 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? Quote Link to comment Share on other sites More sharing options...
ianco Posted November 23, 2011 Author Share Posted November 23, 2011 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. Quote Link to comment Share on other sites More sharing options...
joe92 Posted November 23, 2011 Share Posted November 23, 2011 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. Quote Link to comment Share on other sites More sharing options...
ianco Posted November 23, 2011 Author Share Posted November 23, 2011 Awesome, thanks very much!!! Naughty little asterisk!!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.