/    Sign up×
Community /Pin to ProfileBookmark

Simple regular expression

Hi there,

I’m new to regular expressions. I’d really appreciate it if anyone could offer some assistance on a presumably simple regular expression that I can’t seem to figure out.

I’m looking to insert a raquo just before the </p> of the last paragraph in a string to suggest that an article is continued. In so many words, I’d like…

[code]
<p>This is the first paragraph.</p>

<p>This is the second and last paragraph.</p>
[/code]

… to become…

[code]
<p>This is the first paragraph.</p>

<p>This is the second and last paragraph. &raquo;</p>
[/code]

I’ve been using preg_replace in an attempt to accomplish this seemingly simple task, but to little avail. Apparently it applies to some strings, but not all – I’ve no idea why.

[code]preg_replace(“/(.*)</p>$/”, “$1 &raquo;</p>”, $entry->introduction);[/code]

If anyone could shed some lights on why exactly the pattern needs to be enclosed in / as well, I’d appreciate.

Thanks! πŸ™‚

to post a comment
PHP

20 Comments(s) ↴

Copy linkTweet thisAlerts:
@AaliyahRomaMay 13.2009 β€”Β You can use # or ~ instead of / . It helps to seperate modifiers from pattern. See short examples about regex.
Copy linkTweet thisAlerts:
@NogDogMay 13.2009 β€”Β I'd just use str_ireplace() in this case (or str_replace()) if using PHP 4):
[code=php]
$text = str_ireplace('</p>', '&raquo;</p>', $text);
[/code]

If you really feel the urge to use preg_replace(), there's no need to match/copy the stuff before the closing tag, and in fact the closing tag could be a look-ahead assertion:
[code=php]
$text = preg_replace('#(?=</p>)#i', '&raquo;', $text);
[/code]
Copy linkTweet thisAlerts:
@FRKTauthorMay 13.2009 β€”Β I'd just use str_ireplace() in this case (or str_replace()) if using PHP 4):
[code=php]
$text = str_ireplace('</p>', '&raquo;</p>', $text);
[/code]

[/QUOTE]


No can do - there are several paragraphs, and I'd only like to put a raquo after the last one.

If you really feel the urge to use preg_replace(), there's no need to match/copy the stuff before the closing tag, and in fact the closing tag could be a look-ahead assertion:
[code=php]
$text = preg_replace('#(?=</p>)#i', '&raquo;', $text);
[/code]
[/QUOTE]


This made no sense to me, but I'll see if it works. ?
Copy linkTweet thisAlerts:
@FRKTauthorMay 13.2009 β€”Β [code=php]$text = preg_replace('#(?=</p>)#i', '&raquo;', $text); [/code]

This appends &raquo; before every </p> tag, which is not what I want. I tried to implement $ to correct this with your code, but to no avail.

... and why is it now that the regular expression is wrapped within # rather than /? I know regular expressions are confusing, but this...!
Copy linkTweet thisAlerts:
@NogDogMay 13.2009 β€”Β Try this:
[code=php]
$text = preg_replace('#(?=</p>s*$)#i', '&raquo;', $text);
[/code]
Copy linkTweet thisAlerts:
@FRKTauthorMay 13.2009 β€”Β Parse error: syntax error, unexpected '=' in /Library/WebServer/Documents/Sites/forrykt/online/index.php on line 72


:/
Copy linkTweet thisAlerts:
@NogDogMay 13.2009 β€”Β Worked OK for me:
[code=php]
<?php
$text = "<p>this is a test</p><p>it is only a test</p>";
$text = preg_replace('#(?=</p>s*$)#i', '&raquo;', $text);
echo htmlspecialchars($text);
?>
[/code]
Copy linkTweet thisAlerts:
@FRKTauthorMay 13.2009 β€”Β I'm sorry, my bad - it does work. Well, nearly.

http://www.forrykt.com/

I applied your regular expression to both the first and the second entry, however it doesn't seem to work in the case of the second one. Perhaps it's because it contains links?
Copy linkTweet thisAlerts:
@NogDogMay 14.2009 β€”Β Yet another case of requirements creep. :p
[code=php]
$text = preg_replace('#</p>(?!.*</p>)#i', '&raquo;</p>', $text);
[/code]
Copy linkTweet thisAlerts:
@FRKTauthorMay 14.2009 β€”Β That doesn't work either. It creates a raquo for every paragraph in the first case, and none in the second.

I'm sorry, I assumed this would be a simple regular expression. Thanks for your help anyway. ?
Copy linkTweet thisAlerts:
@NogDogMay 14.2009 β€”Β Sorry, I tested with a sinlge-line string. Add the "s" modifier:
<i>
</i>'#&lt;/p&gt;(?!.*&lt;/p&gt;)#i[B][COLOR="Red"]s[/COLOR][/B]'
Copy linkTweet thisAlerts:
@FRKTauthorMay 14.2009 β€”Β ... still doesn't add a raquo to the second case (as seen on www.forrykt.com). Nevermind though, it's no big deal. I was planning to look into regular expressions but after this I don't think I'm going to bother.

?
Copy linkTweet thisAlerts:
@NogDogMay 14.2009 β€”Β This was my test case, so that you can see if I'm understanding the requirement or not.
[code=php]
<pre><?php
$text = <<<EOD
<p>This is a test.</p>
<p>It is only a test</p>
<div>I am not a paragraph.</div>
EOD;

$text = preg_replace('#</p>(?!.*</p>)#is', '&raquo;</p>', $text);
echo htmlspecialchars($text);
?></pre>
[/code]

Output;
<i>
</i>&lt;p&gt;This is a test.&lt;/p&gt;
&lt;p&gt;It is only a test&amp;raquo;&lt;/p&gt;
&lt;div&gt;I am not a paragraph.&lt;/div&gt;
Copy linkTweet thisAlerts:
@FRKTauthorMay 14.2009 β€”Β Edit.
Copy linkTweet thisAlerts:
@CharlesMay 14.2009 β€”Β Regular expressions really aren't well suited to monkeying with HTML except for the most simple and constrained examples. If your HTML is or can be XHTML it is better to parse it.
Copy linkTweet thisAlerts:
@FRKTauthorMay 14.2009 β€”Β Regular expressions really aren't well suited to monkeying with HTML except for the most simple and constrained examples. If your HTML is or can be XHTML it is better to parse it.[/QUOTE]

Alright, that's fair.
Copy linkTweet thisAlerts:
@FRKTauthorMay 14.2009 β€”Β Edit.
Copy linkTweet thisAlerts:
@FRKTauthorMay 14.2009 β€”Β I restarted Apache and somehow now your latest edition of the regular expression works. Thanks so much NogDog.
Copy linkTweet thisAlerts:
@NogDogMay 14.2009 β€”Β I restarted Apache and somehow now your latest edition of the regular expression works. Thanks so much NogDog.[/QUOTE]

You're welcome.

BTW, while I agree with Charles in general principle, in practical fact it can be so much easier to do with a regexp and a single function call, that I will often take the easy way out, especially if it is not a mission-critical functionality.
Copy linkTweet thisAlerts:
@CharlesMay 14.2009 β€”Β For the simple and constrained, I also wimp out and just use regular expressions. But easy parsing is what XHTML is for.
Γ—

Success!

Help @FRKT spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 β€” BETA 5.18,
whats_new: community page,
up_next: more Davinciβ€’003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinciβ€’003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...