By the_crouton

2019-03-14 19:54:06 8 Comments

I am trying to search through html tags with PHP and I cannot seem to get the regex correct, I am not sure what I am doing wrong. Here is the pattern I am trying to search through:

<cas:serviceResponse xmlns:cas=''>

I used $resp = htmlentities(file_get_contents($url)); and the above prints out if I echo $resp. I am trying to use preg_match to search in cas:user to extract the username , user29.

Here is the regex pattern I am trying to use:

preg_match("'<cas:user>(.*?)</cas:user>'", $resp, $match);

But it doesn't seem to work when I echo $match[1]. What am I doing wrong?


@ALFA 2019-03-14 20:15:09

You're parsing XML with regex which is not the best option. However if you MUST use regular expressions try this:

preg_match("/<cas:user>(.*?)<\/cas:user>/", $resp, $match);


You code works, try echo $match[1];. Thanks to @Barmar.

@Barmar 2019-03-14 20:17:43

You need delimiters around the regexp.

@Barmar 2019-03-14 20:19:58

In fact, the quotes that he used were being used as the delimiters. His code works.

@ALFA 2019-03-14 20:20:02

You're right, edited now :) Edit again: you're right again. Didn't try it but it actually works.

@Barmar 2019-03-14 20:22:10

echo $match[1]; is probably what he really wants.

@the_crouton 2019-03-14 20:31:19

I have also tried $match[1] and it doesn't work. Will edit my question

@ALFA 2019-03-14 20:32:23

Can you share more code please?

@the_crouton 2019-03-18 16:16:34

Would it not be working due to the namespaces?

@miken32 2019-03-14 20:13:48

You shouldn't try parsing XML with regular expressions. Instead use a DOM parser like so:

$xml = <<< XML
<?xml version="1.0"?>
<cas:serviceResponse xmlns:cas=''>

$dom = new DomDocument();
$xpath = new DomXPath($dom);
$node = $xpath->query("//cas:user");
if ($node->length) {
    echo $node[0]->textContent;

This could be done with less code in a flat XML document, but namespaces complicate things a bit and make it easier to use XPath.

Related Questions

Sponsored Content

30 Answered Questions

[SOLVED] startsWith() and endsWith() functions in PHP

  • 2009-05-07 12:14:27
  • Click Upvote
  • 766377 View
  • 1340 Score
  • 30 Answer
  • Tags:   php string

18 Answered Questions

[SOLVED] Reference — What does this symbol mean in PHP?

15 Answered Questions

[SOLVED] Why shouldn't I use mysql_* functions in PHP?

  • 2012-10-12 13:18:39
  • Madara Uchiha
  • 201877 View
  • 2367 Score
  • 15 Answer
  • Tags:   php mysql database

12 Answered Questions

[SOLVED] Can you provide some examples of why it is hard to parse XML and HTML with a regex?

  • 2009-03-31 14:13:39
  • Chas. Owens
  • 40461 View
  • 390 Score
  • 12 Answer
  • Tags:   html xml regex

1 Answered Questions

[SOLVED] PHP preg_match not working as HTML5 pattern

  • 2014-02-10 10:26:29
  • user3243925
  • 665 View
  • 1 Score
  • 1 Answer
  • Tags:   php regex html5

2 Answered Questions

[SOLVED] Using delimiters with preg_match

  • 2013-11-20 19:16:40
  • user2650277
  • 53 View
  • 0 Score
  • 2 Answer
  • Tags:   php preg-match

0 Answered Questions

Regular expression to preg_match longitude and latitude in scraped html

3 Answered Questions

[SOLVED] php preg_match - multivalue pattern

3 Answered Questions

[SOLVED] Extract value from a multiline pattern using PHP and preg_match

2 Answered Questions

Using preg_match to extract a table

  • 2012-02-09 15:16:53
  • user1197941
  • 1277 View
  • 0 Score
  • 2 Answer
  • Tags:   php regex

Sponsored Content