Page 1 of 1
string manuplation
Posted: Mon Nov 27, 2006 3:23 am
by itsmani1
in below mentioned strings i want to remove everything after .html
xxxxxxx.html?xxx=xxxx
xxxxxxx.html?xxx=xxxx&xxx=xxxxxx
how can i do this?
i don't want to hard code it.
combine two functions
Posted: Mon Nov 27, 2006 5:12 am
by Jaxolotl
with this "stupid" but fast script you may do that
Code: Select all
$string = "xxxxxxx.html?xxx=xxxx&xxx=xxxxxx.html.html";
/*
the function first check if you feed it with a string containing characters
if not it returns an error message
then, if it finds ".html" on the string
replace all the content from the fist match to the end with ".html"
if not match it returns the string as is
*/
function my_replacement($my_string){
if($my_string){
$limit = @strpos($my_string, ".html",0);
if($limit){
return substr_replace($my_string, ".html",$limit);
}
else{
return $my_string;
}
}
else{
return "no string has been given";
}
}
echo my_replacement($string);
note that it stops at the first match of ".html"
you may inprove it by using switch inside instead of it so if no argument was givent to the functions you wont go on error
http://it2.php.net/manual/en/function.strpos.php
http://it2.php.net/manual/en/function.s ... eplace.php
Posted: Mon Nov 27, 2006 5:21 am
by CoderGoblin
http://www.php.net/manual/en/function.preg-replace.php
Code: Select all
$var=preg_replace('/(.+html\?).*/iu',"${1}",$orig_string");
or something similar...
Posted: Mon Nov 27, 2006 5:37 am
by aaronhall
Maybe a quicker way of doing it:
Code: Select all
$str = "asdf.html?xxx=xxxx&xxx=xxxx";
$str_chopped = substr($str, 0, strpos($str, '?'));