PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
itsmani1
Forum Regular
Posts: 791 Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:
Post
by itsmani1 » Fri Mar 13, 2009 6:37 am
When i try this I get output:
Code: Select all
$subject = 'A.G. Currie Middle School';
$pattern = '/^A(.*)l$/';
preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE);
echo '<pre>';
print_r($matches);
But no output with this:
Code: Select all
$subject = '<p><span class="Title">A.G. Currie Middle School</span><br />';
$pattern = '/^A(.*)l$/';
preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE);
echo '<pre>';
print_r($matches);
I want to catch data between "<p><span class="Title">" and "</span><br />"
what is the reason I can't get output?
thanks
VladSun
DevNet Master
Posts: 4313 Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria
Post
by VladSun » Fri Mar 13, 2009 6:45 am
^ means the very beginning of the string (line)
$ means the end of the string (line)
And in <p><span class="Title">A.G. Currie Middle School</span><br /> , the letter A is not at the beginning of the string.
There are 10 types of people in this world, those who understand binary and those who don't
itsmani1
Forum Regular
Posts: 791 Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:
Post
by itsmani1 » Fri Mar 13, 2009 6:51 am
thanks but tried this and this is also not working. giving error
Warning: preg_match() [function.preg-match]: Unknown modifier 'p' in C:\wamp\www\cada1.php on line 9
Code: Select all
$subject = '<p><span class="Title">A.G. Currie Middle School</span><br />';
$pattern = '/^<p><span class="Title">(.*)</span><br />$/';
preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE);
echo '<pre>';
print_r($matches);
exit;
VladSun
DevNet Master
Posts: 4313 Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria
Post
by VladSun » Fri Mar 13, 2009 7:29 am
Change begin-end pattern char to something else ('/' presents in the HTML content) or escape '/'.
E.g.
$pattern = '#patternHere#'
There are 10 types of people in this world, those who understand binary and those who don't
itsmani1
Forum Regular
Posts: 791 Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:
Post
by itsmani1 » Fri Mar 13, 2009 7:54 am
Hi thanks, I am able to fix it.
I wanted to now fetch "Middle School" from this string but getting any output, from where I can get help in it. I think my code it correct but its not working, is there any formula for this?
Here is my code
Code: Select all
$data = '<p><span class="Title">A.G. Currie Middle School</span><br>Middle School<br>County: Orange<br>Area: F</p>';
$pattern = '/<\/span><br>([A-Za-z ]+)<br>/'; //To get school
preg_match_all($pattern, $data, $matche1);
echo '<pre>'; print_r($matche1); exit;
itsmani1
Forum Regular
Posts: 791 Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:
Post
by itsmani1 » Fri Mar 13, 2009 8:18 am
This worked for school
Code: Select all
$pattern = '/<\/span><br \/>([A-Za-z ]+)<br \/>/'; //To get school
I tired this but failed:
Code: Select all
$pattern = '/<br>County: ([A-Za-z ]+)<br>/'; //To get county
Tried this for area but failed:
Code: Select all
$pattern = '/<br>Area: ([A-Za-z ]+)<\/p>/'; //To get area
VladSun
DevNet Master
Posts: 4313 Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria
Post
by VladSun » Fri Mar 13, 2009 8:51 am
How about putting them into a single regexp:
Code: Select all
#<p><span class="Title">(.+)</span><br>(.+)<br>County: (.+)<br>Area: (.+)</p>#
There are 10 types of people in this world, those who understand binary and those who don't
itsmani1
Forum Regular
Posts: 791 Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:
Post
by itsmani1 » Fri Mar 13, 2009 9:06 am
nope did not worked.