Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
20.9.2014 - We have just updated our course layouts and descriptions and added our 2015 schedule.

Regular exressions in PHP

Posted by miyagi (miyagi), 21 December 2002
I have some HTML which looks broadly like that below.
I need to change the text and URL to a common reference in all places.
I don't seem to be able to get the ereg_replace to work and need some pointers.......
The lines I want to change look similar to this:

Try <a href=a long and complicated URL goes here and is always unique</a>: <font><b>Unique text</b></font><br clear=all>

I've tried, variations on:
$CleanCode = ereg_replace ("Try(.*)<br clear=all>", "Replacement text and URL ", $DataIn);

What I get is all the code gets cahnged, I guess due to the (.*) - how can I change all the required instances and leave the bits in the middle ??

---Code snipet---
<table width=100%><tr valign=top>
<td width=50% class="small">
<font face="verdana, arial, helvetica" size="-1">
text
</font>
</td>
<td>
<form></form> <br>
<font>
Try <a href=a long and complicated URL goes here and is always unique</a>: <font><b>Unique text</b></font><br clear=all>
</font>
</td>
</table>
<table width=100%><tr valign=top>
<td width=50% class="small">
<font face="verdana, arial, helvetica" size="-1">
text
</font>
</td>
<td>
<form></form> <br>
<font>
Try <a href=This is different but is still a long and complicated URL goes here and is always different</a>: <font><b>Second Unique text</b></font><br clear=all>
</font>
</td>
</table>
<table width=100%><tr valign=top>
<td width=50% class="small">
<font face="verdana, arial, helvetica" size="-1">
text
</font>
</td>
<td>
<form></form> <br>
<font>
Try <a href=The third long and complicated URL goes here which is always unique</a>: <font><b>Unique text goes here</b></font><br clear=all>
</font>
</td>
</table>
Thanks for visiting type of text
---end code---


Posted by admin (Graham Ellis), 22 December 2002
I think that your problem here is that you used (.*) rather than (.*?).

.* matches 0 or more characters, and is greedy.  In other words, it keeps on going as long as it can, so it will match from the first Try right through to the last<br clear=all> in your incoming string.

.*? matches 0 or more characters, and is sparse.  It matches as few characters as it can, so its initial match is from the first Try through to the first<br clear=all> thereafter.

You might also like to look at preg_replace, which the ereg_replace manual tells us is an often-faster alternative.



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho