0

Using regex in PHP how can I get the 108 from this tag?

<td class="registration">108</td>
4
  • What do you actually mean by get it from the tag? Commented Sep 26, 2013 at 14:55
  • I think this is stored in a string, else it has nothing to do with PHP. Commented Sep 26, 2013 at 14:56
  • @Rafael I want using regex, to get the number 108 in the example, or any data (letters or not) between the td class registration Commented Sep 26, 2013 at 14:57
  • @Kaoukkos Don't use regex to parse html. Amal has a good solution for your problem Commented Sep 26, 2013 at 15:01

2 Answers 2

4

Regex isn't a good solution for parsing HTML. Use a DOM Parser instead:

$str = '<td class="registration">108</td>';    
$dom = new DOMDocument();
$dom->loadHTML($str);      

$tds = $dom->getElementsByTagName('td');
foreach($tds as $td) { 
    echo $td->nodeValue; 
}

Output:

108

Demo!

The above code loads up your HTML string using loadHTML() method, finds all the the <td> tags, loops through the tags, and then echoes the node value.


If you want to get only the specific class name, you can use an XPath:

$dom = new DOMDocument();
$dom->loadHTML($str);      

$xpath = new DomXPath($dom);

// get the td tag with 'registration' class
$tds = $xpath->query("//*[contains(@class, 'registration')]");

foreach($tds as $td) { 
    echo $td->nodeValue;
}

Demo!

This is similar to the above code, except that it uses XPath to find the required tag. You can find more information about XPaths in the PHP manual documentation. This post should get you started.

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

3 Comments

Thank you but I may need to use a lot of different td classes. So how can I do this more specific to the specific td class?
@Kaoukkos: that makes it even more appropriate to use the DOM parser rather than regex. The DOM parser is a much better tool for selecting specific elements; that's what it's built for. Regex will burn you if you try parsing complex HTML.
Thank you, I will try it in a while!
-1

If you wish to force regex, use the <td class=["']?registration["']?>(.*)</td> expression

7 Comments

Thank you for this but I need to have it specific for the exact <td class="registration">. Not for every td available in the code
Because he used ^ and $.
Warning: preg_match_all(): No ending delimiter '^'
Exactly... remove those.
@Kaoukkos You have to use delimiters to wrap regexp in preg_*.
|

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.