Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regular expression question 1

Status
Not open for further replies.

elck

Programmer
Apr 19, 2004
176
NL
Hi guys & galls,

I am an oldfashioned basic programmer doing not too badly with php, but regular expressions are a mystery to me, although I can see its advantages!

For instance, I now want to replace in a text a token like this:
[photo:1] some text [photo:01256] some more text

with this:

<img src='..\pics\img1.jpg' > some text <img src='..\pics\img01256.jpg' > some more text

Is there an easy way of doing this using ereg_replace?

Thanks on beforehand


 
Don't feel bad about not understanding regular expressions. I've been using them for years and I don't understand them.

I generally don't use the ereg family of functions. I've found the preg family of functions has more functionality.


The pattern for which you're looking is very regular, to it makes the pattern pretty simple.

Code:
<?php
$a = '[photo:1] some text [photo:0a1256] some more text';

$b = preg_replace ('/\[photo:([^\]]*)\]/', '<img src="../pics/img$1.jpg">', $a);

print $b;
?>



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Ah it looks so complex because I used [, which I presume has special meaning, so you had to escape it.

Maybe one day I can find this sort of expression myself:)

Thanks sleipnir!

 
Correct. Perl-compatible regular expressions (the "preg" part of PHP's "preg" family of functions) uses square brackets to delineate sets of possible elements to match.

For example, the perl-compatible regular expression '/[abcdef]/' will match 'a', 'b', 'c', 'd', 'e' and 'f'.


Notice also the sub-expression in the expression I posted: "[^\]]". The construct "[^abc]" will match anything that is not 'a', 'b' or 'c'.

So "[^\]]" will match anything but the close-square-bracket character.

Expanding, "[^\]]*" matches zero or more non-close-square-bracket characters.

Expanding further, "([^\]]*)" matches the above and also groups it. The "$1" reference in the replacement string is whatever will be matched here.



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Ah great!
When is your course on regular expressions available in print? :)

I truely begin to understand it now!
Thanks again Sleipnir
 
If you wanna understand regex's fully, read up on finite state machines... once you understand those and learn the syntax issues, you're set.

All a regex really is, is a definition of an FSM.

-Rob
 
You probably don't want to read my book

Ok I spotted this .. what book ? :)

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
CORRECTION:
Had I actually written one,[/li] you probably don't want to read my book.


I don't think it's necessary to get into finite state machines to understand regular expressions. Though it would certainly help to give you understanding of their operation.



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top