Question:
PHP trim a word of a string?
2008-05-30 04:01:27 UTC
i got a problem trying to trim a word of a string
$url="http://www.runescapegod.com/watch.php?v=rg93jo";

$trimmed = trim($url, 'http://www.runescapegod.com/watch.php?v=';

I wanna trim of $url so that the output will be on rg93jo
However the code is also trimming of the rgo making the output 93

PLEASE HELP
I WILL ADVERTISE YOUR WEBSITE FOR FREE
Ten answers:
The F
2008-05-30 04:29:01 UTC
trim is not the right solution

you can use strpos, stripos, substr and strstr

if you know the exact location you can use only substr

$str = "This is a string";

$r = substr($str, 0,9); //will give "This is a"

$r = substr($str, 10); //whil give "string"

otherwise first find the position

$start = strpos($str, 'a '); //will give the position of 'a ' - 8

$r = substr($str, $start+2);
heidel
2016-10-06 12:35:50 UTC
Php Trim String
?
2016-12-18 09:49:31 UTC
Php Ltrim
2008-05-30 04:39:29 UTC
Given this code:



$text = "This is a string";

$lookup = " "; // This string contains a single space!

$pos = strlen($text) - 1;

echo substr_rev_pos($text, $lookup, $pos);



the function below will look inside $text for any single character from $lookup, and it will start at position $pos, going back toward the first character.



When it finds such a character, it will return the part of $text that precedes it, including the character that was found.



If it does NOT find such a character, it'll return the first $pos characters of $text.



function substr_rev_pos($string, $needles, $index)

{

  if(strlen($string) > $index)

  {

    for($i = $index - 1; $i >= 0; --$i)

    {

      if(strpos($needles, $string{$i}) !== false)

      {

        return substr($string, 0, $i + 1);

      }

    }

  }



  return substr($string, 0, $index);

}
tutorials
2014-07-29 02:13:26 UTC
Trim function Demo :
itcn
2008-05-30 04:04:47 UTC
Not sure if I understand your question ... looks like the URL was cut off. Do you want to use strstr() to find the string and then substr() to extract a specified substring?
Rav
2008-05-30 04:27:20 UTC
Believe me, trim is not the function you need. Actually I should RTFM you before giving an answer ;-)



//Assumes the "v=something" is at the end of the $url var

$trimmed = preg_replace("/^.*v=/", '', $url );
2016-03-13 09:19:03 UTC
difficult problem. research into a search engine. just that can help!
2014-11-05 00:04:23 UTC
confusing situation. look into over yahoo and bing. this will help!
Tiberiu B
2008-05-30 04:08:25 UTC
yes,you give me advertise.Your name come from a big inventory.step after him


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...