Gaurav Mishra
Gaurav Mishra

Reputation: 135

How to write pattern for preg_match_all in php

I have following html code, I want to extract the name and text in textarea. How can I do it?

Dear You've just received.<br><br><b> Name<br><br>Added:</b> 2012-07-14<br><br>Rating - 46%, Yes - 14, No - 16<br><br><TEXTAREA rows=5 cols=60 wrap>Text in textarea</textarea>

Please tell me how to do it and how to write patterns for preg_match so that I don't need to ask such questions again..

Upvotes: 0

Views: 963

Answers (3)

Truth4oll
Truth4oll

Reputation: 7

You can use simpleHtmlDom for work with html:

include('simple_html_dom.php');
$html = str_get_html("Dear You've just received.<br><br><b> Name<br><br>Added:</b> 2012-07-14<br><br>Rating - 46%, Yes - 14, No - 16<br><br><TEXTAREA rows=5 cols=60 wrap>Text in textarea</textarea>");
echo $textarea = $html->find("textarea", 0)->innertext; //text of textarea
$name= $html->find("b",0)->innertext; //consist: 'Name<br><br>Added:'
preg_match ('/(\w+)/i',$name,$matche); 
echo $name = $matche[0]);

Upvotes: 0

Gaurav Mishra
Gaurav Mishra

Reputation: 135

Got the pattern-

/<br><br><b>(.*?)<br>(.*?)<TEXTAREA(.*?)>(.*?)<\/textarea>/i

Thanks to Truth4oll and Rubular

Upvotes: 1

jozi
jozi

Reputation: 316

Check out some regex simulators such as http://www.lumadis.be/regex/test_regex.php and to build your pattern, take some time to study the Bible http://www.regular-expressions.info/reference.html.

Upvotes: 0

Related Questions