Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Return everything after delimiter

Tags:

string

php

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!

like image 956
Sophivorus Avatar asked Apr 19 '13 12:04

Sophivorus


2 Answers

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
like image 97
anubhava Avatar answered Oct 07 '22 01:10

anubhava


EDIT: this one works fine.

$str = "File:MyFile.jpg";
$str = substr( $str, ( $pos = strpos( $str, ':' ) ) === false ? 0 : $pos + 1 );
like image 36
Ravindra Shekhawat Avatar answered Oct 07 '22 01:10

Ravindra Shekhawat