Home  >  Article  >  Backend Development  >  What should I do if the php regular expression cannot be matched?

What should I do if the php regular expression cannot be matched?

藏色散人
藏色散人Original
2021-05-20 09:23:392256browse

Solution to the problem that php cannot match the regular pattern: 1. Use the preg_match_all() function to obtain the regular matching content; 2. Output the matching content through print_r.

What should I do if the php regular expression cannot be matched?

The operating environment of this article: Windows7 system, PHP7.1 version, DELL G3 computer

Specific questions:

php正则匹配不到
<?php
$url = "http://music.sogou.com/sogou_phb/coo/music/getCampMeta2.jsp?cate=randomsong&count=100&jsoncallback=jsonp1373432530259";
$text = file_get_contents($url);
//正则创建
preg_match("/title\":\"(.*?)\"/", $text, $w);
//echo $text;
echo $w
?>

为什么我这个一个也匹配不到呢?

Solution:

1. To obtain the content matched by the regular expression, you should use the preg_match_all() function

2. It is an array. Arrays cannot be output using echo. If echo outputs an array, it will only display Array

<?php      
 
$url = "
http://music.sogou.com/sogou_phb/coo/music/getCampMeta2.jsp?cate=randomsong&count=100&jsoncallback=jsonp1373432530259
";
 
$text = file_get_contents($url);
 
//正则创建
 
preg_match_all("/title\":\"(.*?)\"/", $text, $w);
 
//echo $text;
 
print_r($w);
 
?>

. Recommended study: "PHP Video Tutorial"

The above is the detailed content of What should I do if the php regular expression cannot be matched?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn