0

How to get text: "Text example max" from:

<td valign="top" align="left">

    <a href="/server?tree=xabaf"
    class="normal"> Text example max </a>

</td>

using regular expression?

include('simple_html_dom.php');
$ch = curl_init('http://www.site.com?id=325235');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$sss = curl_exec($ch);
curl_close($ch);

preg_match_all("#class="normal"?</a>$#", $sss, $arr);
4
  • 1
    So why do you even want to do this with a regular expression? Why not use any of the other available techniques to parse HTML? Commented Jun 15, 2012 at 12:13
  • 3
    Wow, I got an instant flashback of reading stackoverflow.com/a/1732454/813718 Commented Jun 15, 2012 at 12:15
  • Apart from the fact that you shouldn't use regex to parse HTML, you need to show what you have tried doing before expecting others to help. This is not a code generation site. Commented Jun 15, 2012 at 12:18
  • Great, @RemcoOverdijk. thanks for posting that. Commented Jun 15, 2012 at 12:28

3 Answers 3

1

Solution using REGEX

$text = "<a href='/server?tree=xabaf' class='normal'> Text example max </a>
";
$regex_pattern = "/<a href=\"?\'?(.*)\"?\'?>(.*)<\/a>/";
preg_match_all($regex_pattern,$text,$matches);

PHP's DOM

$text = "<a href='/server?tree=xabaf' class='normal'> Text example max </a>";
$dom = new DOMDocument;
$dom->loadHTML($text);
$links = $dom->getElementsByTagName('a');
foreach ($links as $link){
    echo $link->textContent;
}

Use DOM and not regex.

Sign up to request clarification or add additional context in comments.

Comments

0

Since there's no other text, applying strip_tags() is enough.

$str ='<td valign="top" align="left">

    <a href="/server?tree=xabaf"
    class="normal"> Text example max </a>

</td>';

$str = trim(strip_tags($str));

Comments

0

you can try this...

include('simple_html_dom.php');

$url = 'http://www.site.com?id=325235';

$curl = curl_init(); 
curl_setopt($curl, CURLOPT_URL, $url);  
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);  
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);  
$str = curl_exec($curl);  
curl_close($curl);

$html = str_get_html($str);

$content = $html->find('div[class=normal]');
echo $content->innertext;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.