Page 1 of 1

php express problem

Posted: Fri Mar 13, 2009 6:37 am
by itsmani1
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

Re: php express problem

Posted: Fri Mar 13, 2009 6:45 am
by VladSun
^ 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.

Re: php express problem

Posted: Fri Mar 13, 2009 6:51 am
by itsmani1
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;
 

Re: php express problem

Posted: Fri Mar 13, 2009 7:29 am
by VladSun
Change begin-end pattern char to something else ('/' presents in the HTML content) or escape '/'.
E.g.
$pattern = '#patternHere#'

Re: php express problem

Posted: Fri Mar 13, 2009 7:54 am
by itsmani1
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;
 

Re: php express problem

Posted: Fri Mar 13, 2009 8:18 am
by itsmani1
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

Re: php express problem

Posted: Fri Mar 13, 2009 8:51 am
by VladSun
How about putting them into a single regexp:

Code: Select all

#<p><span class="Title">(.+)</span><br>(.+)<br>County: (.+)<br>Area: (.+)</p>#

Re: php express problem

Posted: Fri Mar 13, 2009 9:06 am
by itsmani1
nope did not worked.