PHP RegEx - little help?

Al Neri

Diamond Member
Jan 12, 2002
5,680
1
81
I am trying to get any text that is contained between

"<a target*>" AND "</a>"

* being a wild card

so that each of these would respond with The Quick Brown Fox:

1. <a targetafadfa>The Quick Brown Fox</a>
2. <a target>The Quick Brown Fox</a>
3. <a target in the>The Quick Brown Fox</a>


Now - as I see it -

I would need to tell regex to do the following-

Look for
1. "<a target"
2. a subsequent ">"
3. Grab ANY text following such - until you see "</a>"

I just don't know how to write that in PHP RegEx.

I used txt2re.com but there is no "any text" - so it came up with below (in bold)


<?php
# URL that generated this code:
# http://txt2re.com/index-php.ph...&-47&-37&-2&-43&-6&-38

$txt='<a target test>The Quick Brown Fox</a>';

$re1='(<)'; # Any Single Character 1
$re2='(a)'; # Any Single Character 2
$re3='( )'; # Any Single Character 3
$re4='(target)'; # Word 1
$re5='( )'; # Any Single Character 4
$re6='.*?'; # Non-greedy match on filler
$re7='(>)'; # Any Single Character 5
$re8='.*'; # Non-greedy match on filler
$re9='(<\\/a>)'; # Tag 1

if ($c=preg_match_all ("/".$re1.$re2.$re3.$re4.$re5.$re6.$re7.$re8.$re9."/is", $txt, $matches))
{
$c1=$matches[1][0];
$c2=$matches[2][0];
$c3=$matches[3][0];
$word1=$matches[4][0];
$c4=$matches[5][0];
$c5=$matches[6][0];
$tag1=$matches[7][0];
print "($c1) ($c2) ($c3) ($word1) ($c4) ($c5) ($tag1) \n";
}

echo "<BR><BR><BR><BR>";
echo var_dump($matches);

#-----
# Paste the code into a new php file. Then in Unix:
# $ php x.php
#-----


I added to it the echo vardump of $matches - an attempt to maybe find what I am looking for in the array $matches - but that was to no avail because it outputs (in bold):

(<) (a) ( ) (target) ( ) (>) ()



array(8) { [0]=> array(1) { [0]=> string(38) "The Quick Brown Fox" } [1]=> array(1) { [0]=> string(1) "<" } [2]=> array(1) { [0]=> string(1) "a" } [3]=> array(1) { [0]=> string(1) " " } [4]=> array(1) { [0]=> string(6) "target" } [5]=> array(1) { [0]=> string(1) " " } [6]=> array(1) { [0]=> string(1) ">" } [7]=> array(1) { [0]=> string(4) "" } }



Any help?

-Mat
 

tfinch2

Lifer
Feb 3, 2004
22,114
1
0
If you're just trying to get the in-between text, why you would need a regex to do this?
 

Al Neri

Diamond Member
Jan 12, 2002
5,680
1
81
I'm probably missing something really obvious - how would I do it without regex?
 

troytime

Golden Member
Jan 3, 2006
1,996
1
0
Originally posted by: Don Rodriguez
I'm probably missing something really obvious - how would I do it without regex?

basic string manipulation?
build an array with starts and finishes

loop through array and grab the text

the problem is, if the text block is inconsistent - you're gonna have issues (no matter what method you use)

i like regex and all, but its slow
 

Skeeedunt

Platinum Member
Oct 7, 2005
2,777
3
76
Well, you have to of course assume that the * is anything except a '>', but assuming that, it should be something along the lines of:

preg_match( /<a target[^>]*>([^<]*)<\/a>/, $text );

[^>] being "anything but a >". Not sure if you need to \\\escape any of those characters inside the regex, but I think they're fine.