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:authenticationSuccess> <cas:user>user29</cas:user> </cas:authenticationSuccess> </cas:serviceResponse>
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?
Related Questions
Sponsored Content
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?
30 Answered Questions
[SOLVED] startsWith() and endsWith() functions in PHP
12 Answered Questions
[SOLVED] Can you provide some examples of why it is hard to parse XML and HTML with a regex?
1 Answered Questions
[SOLVED] PHP preg_match not working as HTML5 pattern
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
- 2013-11-04 16:09:33
- Ben Paton
- 300 View
- 0 Score
- 0 Answer
- Tags: php regex preg-match
3 Answered Questions
[SOLVED] php preg_match - multivalue pattern
- 2013-09-12 07:16:05
- gammacode
- 289 View
- 0 Score
- 3 Answer
- Tags: php regex preg-match multivalue and-operator
3 Answered Questions
[SOLVED] Extract value from a multiline pattern using PHP and preg_match
- 2012-07-05 22:29:56
- dvarney
- 1705 View
- 1 Score
- 3 Answer
- Tags: php preg-match multiline
@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:
EDIT
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?
@miken32 2019-03-14 20:13:48
You shouldn't try parsing XML with regular expressions. Instead use a DOM parser like so:
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.