Training, Open Source computer languages

This is page http://www.wellho.net/forum/Writing-PHP/Regular- ... n-PHP.html

Our email: info@wellho.net • Phone: 01144 1225 708225

 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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.

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