Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php preg_替换url以获取id_Php_Url_Curl_Preg Replace - Fatal编程技术网

Php preg_替换url以获取id

Php preg_替换url以获取id,php,url,curl,preg-replace,Php,Url,Curl,Preg Replace,我想用php中的curl替换页面中的URL URL就像 http://www.externalwebsite.com/title-of-the-page-192345.htm 我使用$url=preg\u replace('~a href=“([a-z,.\-]*)~si',''”,$url) 这给了我正确的id,但如果在标题中使用任何其他数字字符 比如, http://www.externalwebsite.com/title-of-the-3-page-192345.htm 它给了我 3

我想用php中的curl替换页面中的URL

URL就像

http://www.externalwebsite.com/title-of-the-page-192345.htm
我使用
$url=preg\u replace('~a href=“([a-z,.\-]*)~si',''”,$url)

这给了我正确的id,但如果在标题中使用任何其他数字字符

比如,

http://www.externalwebsite.com/title-of-the-3-page-192345.htm
它给了我

3-page-192345
输出。在这种情况下,如何获取页面的正确id。多谢各位

更新:

我需要替换curl从另一个站点获取的页面中的URL。URL就像上面写的一样

<?php

$ch = curl_init ("http://www.externalwebsite.com/index.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$page = curl_exec($ch);
preg_match('#<div class="headline"[^>]*>(.+?)</div>#is', $page, $matches);
foreach ($matches as &$match) {
    $match = $match;
}
$html=$matches[1];   
$html = preg_replace('~a href="([a-z,.\-]*)~si', '"', $html); //NEED TO CHANGE THIS                                         

    echo $html;

?>

使用preg\u match代替preg\u replace

<?php

  $matches = array();
  $url ='http://www.mywebsite.com/title-of-the-page-192345.htm';
  preg_match('#http://(.*?)/(.*?)-([0-9]+).htm#', $url, $matches);
  print_r($matches);
  echo $matches[2]; //this will print title of page
  echo $matches[3]; //this will print id of page
  echo $matches[1]; //this will domain
?>

Preg_replace,顾名思义,替换您想要的字符串以获取一些字符串信息。子模式可以在
$matches
数组中获取这些信息。数字的子模式为
([0-9]+)
,表示至少有一个数字

使用preg\u match代替preg\u replace

<?php

  $matches = array();
  $url ='http://www.mywebsite.com/title-of-the-page-192345.htm';
  preg_match('#http://(.*?)/(.*?)-([0-9]+).htm#', $url, $matches);
  print_r($matches);
  echo $matches[2]; //this will print title of page
  echo $matches[3]; //this will print id of page
  echo $matches[1]; //this will domain
?>

Preg_replace,顾名思义,替换您想要的字符串以获取一些字符串信息。子模式可以在
$matches
数组中获取这些信息。数字的子模式为
([0-9]+)
,表示至少有一个数字

您只需要
.htm
之前的最后一个数字?需要调整RegExp我想。。。取消冻结模式匹配或类似的
*-([0-9])+\.htm
您只需要
.htm
之前的最后一个数字?需要调整RegExp我想。。。ungreedy pattern match或类似的
*-([0-9])+\.htm
谢谢,但我必须更改我网站页面的URL,我正在使用它获取另一个网站页面内容,以便在我的网站上阅读。要做到这一点,我必须获取页面的特殊id,并将其替换为我的URL。谢谢,但我必须将URL更改为我的网站页面,我正在使用它获取另一个网站页面内容,以便在我的网站上阅读。要做到这一点,我必须获得页面的特殊id,并将它们替换为我的URL。
<?php

  $matches = array();
  $url ='http://www.mywebsite.com/title-of-the-page-192345.htm';
  preg_match('#http://(.*?)/(.*?)-([0-9]+).htm#', $url, $matches);
  print_r($matches);
  echo $matches[2]; //this will print title of page
  echo $matches[3]; //this will print id of page
  echo $matches[1]; //this will domain
?>
Array ( [0] => http://www.mywebsite.com/title-of-the-page-192345.htm [1] => www.mywebsite.com [2] => title-of-the-page [3] => 192345 )