You are viewing our Forum Archives. To view or take place in current topics click here.
Remove command for php?
Posted:

Remove command for php?Posted:

J2O
  • TTG Champion
Status: Offline
Joined: Dec 28, 200914Year Member
Posts: 8,537
Reputation Power: 405
Status: Offline
Joined: Dec 28, 200914Year Member
Posts: 8,537
Reputation Power: 405
Just looking on how to remove part of a variable;


echo $url


Let's say $url = HAM_SANDWICH
& I want to remove _SANDWICH

how would I go about this?

Thanks.
#2. Posted:
Zoo
  • TTG Addict
Status: Offline
Joined: Aug 13, 201113Year Member
Posts: 2,113
Reputation Power: 103
Status: Offline
Joined: Aug 13, 201113Year Member
Posts: 2,113
Reputation Power: 103
I don't know surely if this works, but this should for what you want..


/* the function */
function remove_querystring_var($url, $key) {
  $url = preg_replace('/(.*)(?|&)' . $key . '=[^&]+?(&)(.*)/i', '$1$2$4', $url . '&');
  $url = substr($url, 0, -1);
  return $url;
}

/* usage */
//pretending this page is


I only have basic PHP knowledge, never really liked it
#3. Posted:
ssshenkie
  • TTG Senior
Status: Offline
Joined: Jan 13, 201113Year Member
Posts: 1,572
Reputation Power: 67
Status: Offline
Joined: Jan 13, 201113Year Member
Posts: 1,572
Reputation Power: 67
Easy:


$url = HAM_SANDWICH
$new_url = str_replace('_SANDWICH', '', $url);
echo $new_url;



Don't use preg_replace if you aren't doing regular expressions, it will only slow down your script!

To learn about the str_replace function go here:
[ Register or Signin to view external links. ]
#4. Posted:
Nic
  • Retired Staff
Status: Offline
Joined: Jun 08, 201014Year Member
Posts: 2,466
Reputation Power: 1070
Motto: I've been watching you all day.
Motto: I've been watching you all day.
Status: Offline
Joined: Jun 08, 201014Year Member
Posts: 2,466
Reputation Power: 1070
Motto: I've been watching you all day.
To me it is not really clear what you want to be removed exactly.
I know what you want to be removed now, but can the variable change?

- Is it just "_SANDWICH" in any URL?
- Is it anything after an underscore in a URL? (then HAM_CHEESE would also become just HAM)

Depending on which you could use the [ Register or Signin to view external links. ] function or perform a regular expression with [ Register or Signin to view external links. ] .


ssshenkie wrote Don't use preg_replace if you aren't doing regular expressions, it will only slow down your script!

It's a bit slower than str_replace, but for simple scripts it honestly doesn't matter, or is noticeable for that matter. I understand it becomes "an issue" with large websites that use it a lot.
Not to mention str_replace only gets you so far, preg_replace is capable of doing much more, so which one you need depends on the situation.
#5. Posted:
ODST_107
  • Resident Elite
Status: Offline
Joined: Oct 07, 201014Year Member
Posts: 247
Reputation Power: 9
Status: Offline
Joined: Oct 07, 201014Year Member
Posts: 247
Reputation Power: 9
You could also use the "split" method if you want to keep what you'r removing for later use.
it would go something like:

<?php//split example
$url = "HAM_SANDWICH";
$url_array = split("_", $url);//split the string up into an array
$url = $url_array[0];//just keep the first part
echo $url;//And now it will just say "HAM"
?>


That's one way you could do it, but it could get tricky if there are more than one of the symbols or patterns you use to split it by in the actual string.
#6. Posted:
speed
  • V5 Launch
Status: Offline
Joined: Jun 11, 200915Year Member
Posts: 9,897
Reputation Power: 3160
Motto: "I'l no I grew up to fast speed I no u will be little famous" - Famous_Energy
Motto: "I'l no I grew up to fast speed I no u will be little famous" - Famous_Energy
Status: Offline
Joined: Jun 11, 200915Year Member
Posts: 9,897
Reputation Power: 3160
Motto: "I'l no I grew up to fast speed I no u will be little famous" - Famous_Energy

str_replace("_SANDWICH", "", $url);
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.