在php的开头和结尾搜索特定关键字的文本

时间:2011-02-18 08:07:09

标签: php

我有以下字符串

$url_part = 'p=link&u=shuja&sort=recent';

我希望以p =开头字符串并以&结尾以这种方式,

$page = 'link'; 

它可能存在于$ url_parts中的任何位置。然后删除相同的

$url_part = 'u=shuja&sort=recent';

4 个答案:

答案 0 :(得分:0)

$url_part = 'p=link&u=shuja&sort=recent';
$start=strpos($url_part,"p=");
$end=strpos($url_part,"&",$start);
$my_value=substr($url_part,$start+2,$end-2) // get link
$url_part=strstr($url_part, $my_value,true); // remove unwnated url // this line only for PHP 5.3

答案 1 :(得分:0)

$url_part = 'p=link&u=shuja&sort=recent';
$params = array();
parse_str($url_part, $params);
$page = isset($params['p']) ? $params['p'] : null;
unset($params['p']);
$url_part = http_build_query($params);

答案 2 :(得分:0)

嗨,你可以像这样使用爆炸

$url=explode('&',$url_part);
$page='';
for($i=0;$i<count($url);$i++) {
    $p=explode('=',$url[$i]);

    if($p[1]=='link')
        $page='link';
    break;
}

答案 3 :(得分:0)

尝试这个

<?php
$url_part = 'p=link&u=shuja&sort=recent';
//use this function instead to extract url_part to an array of argument
parse_str($url_part,$output);
//remove argument 'p' from array
unset($output['p']);
//create the url query again
$url_part = http_build_query($output); 
//will output string "u=shuja&sort=recent"
echo $url_part;
?>