Forum Moderators: coopster

Message Too Old, No Replies

eregi query

         

PeteM

8:43 pm on Nov 11, 2004 (gmt 0)

10+ Year Member



I have a piece of code to extract and print some actors names..........

if (eregi('<actor>(.*)</actor>',$record,$actor)) {
print ('<b>Starring:</b> '.$actor[1]); }

.....from the following piece of XML......

<ItemAttributes>
<Actor>Tobey Maguire</Actor>
<Actor>Kirsten Dunst</Actor>
<Actor>Alfred Molina</Actor>
</ItemAttributes>

Problem is the result looks like this:-

Starring: Tobey MaguireKirsten DunstAlfred Molina

Note the lack of spaces. How can I add a space in between each name?

Thanks, Pete

dreamcatcher

9:19 pm on Nov 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



[edit]Oops, sorry, ignore last post!

coopster

10:38 pm on Nov 11, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Have you done a "View Source" in your browser on that yet? I don't think it is doing what you want yet anyway. Here it is...
<b>Starring:</b> Tobey Maguire</Actor> 
<Actor>Kirsten Dunst</Actor>
<Actor>Alfred Molina
If you want just the names peeled out, you'll have to change your regular expression. I use preg_match_all [php.net] rather than ereg, it's faster. The Uis switches tell it to be Ungreedy, ignore case, and if the $record is on more than one line, take the newlines into consideration:
if (preg_match_all('/<actor>(.*)<\/actor>/Uis', $record, $actor)) { 
print ('<b>Starring:</b> '.implode(' ', $actor[1]));
}
implode() [php.net] is a slick way to put the spaces in ;)

PeteM

11:09 pm on Nov 11, 2004 (gmt 0)

10+ Year Member



Thanks Coopster, I'll give it a go tomorrow.

Pete

PeteM

5:51 pm on Nov 12, 2004 (gmt 0)

10+ Year Member



Thanks, worked perfectly but I don't understand the significance of the first '/' and the first '\'.....

(preg_match_all('/<actor>(.*)<\/actor>/Uis', $record, $actor))

Can you explain?

Many thanks, Pete

coopster

7:00 pm on Nov 12, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



The slashes are delimiters. I usually use the slash (/) character as a delimiter and if you need to use the delimiter character in an expression, it must be escaped with a backslash.

Resource:
PCRE [php.net]