PDA

View Full Version : how to remove this string using regex



smansakra
08-25-2011, 09:10 PM
look,
i have a variable that contain dynamic value,

for example i have $url,
sometimes, this $url contain a value : http://mysite.com/index.php
sometimes, this $url contain a value : http://mysite.com/index.php/login/

i just want to replace "index.php" and "index.php/"
so the result like below:
http://mysite.com/
http://mysite.com/login/

i have just used str_replace("index.php", "",$url) but it doesnt solve my problem

JShor
08-25-2011, 10:34 PM
Try:



$url = str_replace("index.php", "", $url);

smansakra
08-26-2011, 01:06 PM
Your code just result like below
http://mysite.com/
http://mysite.com//login/

i want like this
http://mysite.com/
http://mysite.com/login/

how can i do this?

JShor
08-26-2011, 01:27 PM
Wait, why didn't you just add the forward slash in the removal attribute?

Try this?


$url = str_replace("/index.php", "", $url);

smansakra
08-26-2011, 05:01 PM
Your last code will be like below
http://mysite.com
http://mysite.com/login/

but i want the code like below
http://mysite.com/
http://mysite.com/login/

:'(

JShor
08-26-2011, 05:20 PM
????

Try this...


$url = str_replace("index.php/", "", $url);


Please read this. It will teach you how to use str_replace().
http://php.net/str_replace

smansakra
08-27-2011, 12:54 AM
that will return

http://mysite.com/index.php
http://mysite.com/login/


i want
http://mysite.com/
http://mysite.com/login/

how?

bluewalrus
08-27-2011, 01:09 AM
Try, this will do any address not just login


$url = preg_replace("/(.*?\.com/)(.*)\..*/", "$1$2/", $url);

james438
08-27-2011, 08:26 AM
You may want to test your pcre before posting bluewalrus. Or if you are not going to test then say that you have not tested it yet.

I have not done pcre in a while, but try the following:


<?php
$url="http://mysite.com/login/";
$url=preg_replace('/(http:\/\/mysite\.com)(.*?(\/login\/))/',"$1$3",$url);
echo "$url";
?>
I know the above is very sloppy, but I am not aware of all of the parameters of smansakra's $url variable.

This code makes several assumptions about what is contained in $url, but try it out and if you need what I posted to be modified let us know.

smansakra
08-27-2011, 11:34 PM
@bluewalrus and @james348: Thanks for reply i have tried both of your code, but doesn't work,

this is my code:


<?
function menu_top(){
$domain = "http://www.mysite.ext"; //can be mysite.com, mysite.us, mysite.co.id, mysite.web.id, and so on
$menu = array(
'Home' => $domain,
'People' => $domain.'/people/',
'News' => $domain.'/news/',
'Blog' => $domain.'/blog/'
);
$current_page = $domain.$_SERVER['PHP_SELF'];
//this current page always return something like http://www.mysite.ext/index.php/mypage/
//sometimes return http://www.mysite.ext/index.php
//
//home link, in menu shows http://www.mysite.ext/ , but in current_page return http://www.mysite.ext/index.php
//people link, in menu show http://www.mysite.ext/people/, but in current_page return http://www.mysite.ext/incex.php/people/
//and the other link are too
//
/*
so, i want a regex to remove index.php from $current_page,
like:
http://mysite.com/
http://mysite.com/people/

not like:
http://mysite.com/
http://mysite.com//people/

not like:
http://mysite.com
http://mysite.com/people/
*/
$ret ='<ul>';
foreach($menu as $link_text => $link_url){

if( $current_page == $link_url)
$ret .='<li class="link_active">'.$link_text.'</li>';
else
$ret .='<li><a href="'.$link_url.'">'.$link_text.'</a></li>';
}
$ret .='</ul>';

return $ret;
}
?>

bluewalrus
08-28-2011, 12:09 AM
Untested in PHP.


$url = preg_replace('/' . $domain . '.*?\/(.*)\/', $domain . '/$1/', $current_page);

smansakra
08-28-2011, 02:33 AM
i have tried it,
function.preg-replace ]: Unknown modifier '/'
:'(

JShor
08-28-2011, 02:58 AM
Try this:



$url = str_replace("index.php", "", $url);
$url = str_replace("//", "/", $url);

bluewalrus
08-28-2011, 03:00 AM
Opps, forgot to close the last one



$url = preg_replace('/' . $domain . '.*?\/(.*)\//', $domain . '/$1/', $current_page);

That should do it.

james438
08-28-2011, 03:28 AM
I am terrible with functions, but try this:


<?php
$url="http://mysite.com";
$url=preg_replace('/\/index\.php\/?$/',"/",$url);
$url=preg_replace('/\/index\.php\/?/',"/",$url);
$url=preg_replace('/([a-zA-Z])$\/?/',"$1/",$url);
echo "$url";
?>

Jshor, what you propose would also turn http://mysite.com/ into http:/mysite.com/. It still might be possible to do this with str_replace, but I'm not sure how yet.

smansakra
08-28-2011, 11:06 AM
@james, bluwalrus, jshor: Thanks for all reply from you guys,
i have found my self
my be a little poor,



<?
function menu_top(){
$domain = "http://www.mysite.ext"; //can be mysite.com, mysite.us, mysite.co.id, mysite.web.id, and so on
$menu = array(
'Home' => $domain,
'People' => $domain.'/people/',
'News' => $domain.'/news/',
'Blog' => $domain.'/blog/'
);

$current_php_self = str_replace('index.php/', '', $_SERVER['PHP_SELF']);
$current_php_self = str_replace('index.php', '', $current_php_self);

$current_page = $domain.$current_php_self;


//this current page always return something like http://www.mysite.ext/index.php/mypage/
//sometimes return http://www.mysite.ext/index.php
//
//home link, in menu shows http://www.mysite.ext/ , but in current_page return http://www.mysite.ext/index.php
//people link, in menu show http://www.mysite.ext/people/, but in current_page return http://www.mysite.ext/incex.php/people/
//and the other link are too
//
/*
so, i want a regex to remove index.php from $current_page,
like:
http://mysite.com/
http://mysite.com/people/

not like:
http://mysite.com/
http://mysite.com//people/

not like:
http://mysite.com
http://mysite.com/people/
*/
$ret ='<ul>';
foreach($menu as $link_text => $link_url){

if( $current_page == $link_url)
$ret .='<li class="link_active">'.$link_text.'</li>';
else
$ret .='<li><a href="'.$link_url.'">'.$link_text.'</a></li>';
}
$ret .='</ul>';

return $ret;
}
?>

james438
08-28-2011, 06:55 PM
Ah, clever girl :). I can't see anything wrong with that and it is so simple too.