【问题标题】:PHP Regular expression to delete a stringPHP正则表达式删除字符串
【发布时间】:2015-02-11 17:45:44
【问题描述】:

我有一个用于获取数据的小型 PHP 脚本。这里可能有一个正则表达式错误,但我无法解决。

我得到的输出如下所示。如果我尝试 $title[3][1],我会得到以下输出:string(30) "30charstitle"

var_dump($title[3][1]); gives-> string(30) "30charstitle"   
var_dump($title[3][2]); gives-> string(50) "50charstitle"    
var_dump($title[3][3]); gives-> string(100) "100charstitle"

我想从输出中删除字符串部分和双引号,但我卡住了。基本上我只需要标题本身,不包括双引号和字符串长度。

function siteConnect($site) {
        $ch = curl_init();
        $hc = "YahooSeeker-Testing/v3.9 (compatible; Mozilla 4.0; MSIE 5.5; Yahoo! Search - Web Search)";
        curl_setopt($ch, CURLOPT_REFERER, 'http://www.google.com');
        curl_setopt($ch, CURLOPT_URL, $site);
        curl_setopt($ch, CURLOPT_USERAGENT, $hc);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $site = curl_exec($ch);
        curl_close($ch);

        // preg_match
        preg_match_all('@><a itemprop="url" href="http://www.example.com/id/(.*?)/(.*?).html&path=(.*?)"><span itemprop="name">(.*?)</span></a>@',$site,$title);
        $title[3][1] = strip_tags($title[3][1]);
        $title[3][1] = preg_replace('~.*?>~', '', $title[3][1]);
        var_dump($title[3][1]);
    }
    $enter = siteConnect('http://www.example.com/index.php?route=product/category&path=5_6&page=1');

【问题讨论】:

  • 只需将var_dump 替换为echo
  • 我很难过。谢谢你。问题解决了。
  • @salep:别难过 :-)

标签: php regex curl preg-match-all


【解决方案1】:

替换这个:

var_dump($title[3][1]);

那个:

print($title[3][1]);

或者:

echo $title[3][1];

对于多数组更好:

print_r($title);

【讨论】:

    【解决方案2】:

    你只是想做一个查找,对吗?如果我明白你在追求什么。

    也许是类似 .*"(.*)".* 的东西?

    【讨论】:

      猜你喜欢
      • 2012-03-21
      • 2014-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-03
      • 2011-06-28
      • 1970-01-01
      • 2021-05-31
      相关资源
      最近更新 更多