I have a script like this function convertUrl($originalUrl){ $tmp1 = substr($originalUrl,0, strrpos($originalUrl,"/")); $tmp2 = substr($tmp1,0, strrpos($tmp1,"/")); $tmp3 = substr($originalUrl, strrpos($originalUrl,"/"), strlen($originalUrl)); return $tmp2 . $tmp3;} echo convertUrl("siteurl/id124536/05/how_to_make_a_php_site"); Code (markup): The function should remove some specific text from url like: siteurl/id124536**/05**/how_to_make_a_php_site Now I want, when I input siteurl/id124536**/05**/how_to_make_a_php_site like this url script work otherwise not work. [example: when i input siteurl/id124536/how_to_make_a_php_site like this url script not working]
This is for php not javascript section. What are you trying to do? The result of your function is removing "/05" from the string as I understand but in the example that doesn't work you don't have "/05" and the argument is the result you are trying to achieve so I don't get it. Function is not working because the number of slashes is hard coded in the function and if you change this number the function will fail. I think next will be better: function convertUrl($originalUrl){ $urlArray = explode('/', $originalUrl); unset($urlArray[2]); return implode('/', $urlArray); } echo convertUrl("siteurl/id124536/05/how_to_make_a_php_site"); Code (markup): But you have to have logic for different number of slashes function convertUrl($originalUrl){ $urlArray = explode('/', $originalUrl); if (count($urlArray) === 4 ) { unset($urlArray[2]); } elseif (count($urlArray) === 3) { unset what pat of the url you want } return implode('/', $urlArray); } echo convertUrl("siteurl/id124536/05/how_to_make_a_php_site"); Code (markup):
Second function is not working because is not finished. You had to tell me what is your goal because I don't understand what the function must do. What result you want from the function? Is this your goal: siteurl/id124536/how_to_make_a_php_site ? Do you want the end result to be URL with 2 slashes "/" like here? Do you know what URLs will be argument of the function? What URLs the function has to change? With how many slashes in the URL? Where is the string you want to remove - always after first slash or always after second slash or the place changes? Is the string you want to remove always the same?
function convertUrl($originalUrl) { $cleanUrl = str_replace(array('http://www.', 'http://'), '', $originalUrl); // remove protocol $urlArray = explode('/', $cleanUrl); // make an array from the string if (count($urlArray) === 4 ) { unset($urlArray[2]); // remove the 3d element if the array has 4 elements } return implode('/', $urlArray); //craete string from the array and return it } echo convertUrl("siteurl/id124536/05/how_to_make_a_php_site"); echo "<br>"; echo convertUrl("siteurl/id124536/how_to_make_a_php_site"); echo "<br>"; echo convertUrl("http://siteurl.com/id124536/05/how_to_make_a_php_site"); Code (markup): results: siteurl/id124536/how_to_make_a_php_site siteurl/id124536/how_to_make_a_php_site siteurl.com/id124536/how_to_make_a_php_site Is this the result you wanted or I didn't understand the purpose of the function?