PHP - how to remove data using PHP
How can I remove ?cat=
from the example 1 so it can lo开发者_开发问答ok like example 2 using PHP.
Example 1
?cat=some-cat
Example 2
some-cat
Easy, use str_replace
:
$cat = '?cat=some-cat';
$cat = str_replace('?cat=', '', $cat);
EDIT:
If you are pulling this query string through something like $_SERVER['QUERY_STRING']
, then I'd opt for you to use $_GET
, which is an associative array of the GET variables passed to your script, so if your query string looked like this:
?cat=some-cat&dog=some-dog
The $_GET
array would look like this:
(
'cat' => 'some-cat',
'dog' => 'some-dog'
)
$cat = $_GET['cat']; //some-cat
$dog = $_GET['dog']; //some-dog
Another edit:
Say you had an associative array of query vars you wish to append onto a URL string, you'd do something like this:
$query_vars = array();
$query_vars['cat'] = 'some-cat';
$query_vars['dog'] = 'some-dog';
foreach($query_vars as $key => $value) {
$query_vars[] = $key . '=' . $value;
unset($query_vars[$key]);
}
$query_string = '?' . implode('&', $query_vars); //?cat=some-cat&dog=some-dog
Yes, you can, very easily:
$cat = '?cat=some-cat';
$cat = substr($cat, 5); // remove the first 5 characters of $cat
This may not be the best way to do this. That will depend on what you are attempting to achieve...
精彩评论