简体   繁体   中英

PHP Regular expression to delete a string

I've a small PHP script that I use to fetch data. Probably there is a regex error here, yet I am unable to solve.

The output I get is something like below. If I try $title[3][1], I get the output following output : 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"

I want to remove the string part and double quotes from the output, but I got stuck. Basically all I need is the title itself, excluding double quotes and length of the string.

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');

Replace this:

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

That:

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

Or:

echo $title[3][1];

For multi-arrays better:

print_r($title);

You'll just want to do a find right? If I understand what you're after.

So something along the lines of .*"(.*)".* perhaps?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM