Sophivorus
Sophivorus

Reputation: 3083

PHP - Return everything after delimiter

There are similar questions in SO, but I couldn't find any exactly like this. I need to remove everything up to (and including) a particular delimiter. For example, given the string File:MyFile.jpg, I need to remove everything up to the :, so that I'm left with only MyFile.jpg. Thanks in advance!

Upvotes: 5

Views: 8477

Answers (7)

John Miller
John Miller

Reputation: 717

Shorter codes:

To return everything BEFORE the FIRST occurence of a character, use strtok. Example:

  • strtok(16#/en/go, '#') will return 16

To return everything AFTER the FIRST occurence of a character, use strstr. Example:

  • strstr(16#/en/go, '#') will return #/en/go (Includes search character '#')
  • substr(strstr(16#/en/go, '#'), 1) will return /en/go

To return everything AFTER the LAST occurrence of a character, use strrchr. Example:

  • strrchr(16#/en/go, '/') will return /go (Includes search character '/')
  • substr(strrchr(16#/en/go/, '/'), 1) will return go

Upvotes: 3

JAY
JAY

Reputation: 21

Sample String:

$string = 'value:90|custom:hey I am custom message|subtitute:array';

convert string to array

$var = explode('|', $string);

Check Results:

Array(
[0] => value:90
[1] => custom:hey I am custom message
[2] => subtitute:array)

Declare an array variable

$pipe = array();

Loop through string array $var

foreach( $var as $key => $value ) {
  // get position of colon
  $position = strrpos( $value, ':' );
  // get the key
  $key = substr( $value, 0, $position );
  //get the value
  $value = substr( $value, $position + 1 );
  $pipe[$key] = $value; }

Final Result:

Array(
[value] => 90
[custom] => hey I am custom message
[subtitute] => array)

Upvotes: 0

Aris
Aris

Reputation: 5057

    $str = "File:MyFile.jpg";

    $position = strpos($str, ':');//get position of ':'

    $filename= substr($str, $position+1);//get substring after this position

Upvotes: 1

Ravindra Shekhawat
Ravindra Shekhawat

Reputation: 4353

EDIT: this one works fine.

$str = "File:MyFile.jpg";
$str = substr( $str, ( $pos = strpos( $str, ':' ) ) === false ? 0 : $pos + 1 );

Upvotes: 4

anubhava
anubhava

Reputation: 785128

Use this preg_replace call:

$str = 'File:MyFile.jpg';
$repl = preg_replace('/^[^:]*:/', '', $str); // MyFile.jpg

OR else avoid regex and use explode like this:

$repl = explode(':', $str)[1]; // MyFile.jpg

EDIT: Use this way to avoid regex (if there can be more than one : in string):

$arr = explode(':', 'File:MyFile.jpg:foo:bar');
unset($arr[0]);
$repl = implode(':', $arr); // MyFile.jpg:foo:bar

Upvotes: 9

pmayer
pmayer

Reputation: 341

Two simple ways:

$filename = str_replace('File:', '', 'File:MyFile.jpg');

or

$filename = explode(':', 'File:MyFile.jpg');
$filename = $filename[1];

Upvotes: 0

Glitch Desire
Glitch Desire

Reputation: 15023

You could use explode to do this: link.

Something like:

$string = "File:MyFile.jpg";
list($protocol,$content) = explode(":", $string);
echo $content;

Upvotes: 1

Related Questions