Php Substring Remove Everything in a String After Certain Character

Remove portion of a string after a certain character

$variable = substr($variable, 0, strpos($variable, "By"));

In plain english: Give me the part of the string starting at the beginning and ending at the position where you first encounter the deliminator.

Remove all characters after a specific character

Shortest one:

echo strtok('test?=new', '?');

If you want to keep the question mark, the solution is almost the same:

echo strtok('test?=new', '?').'?';

Remove portion of a string after a particular character from the end of the string

Use strrpos to get position of last matching element

//$variable = "8233 Station #2212"; //8233 Station
$variable = "8233 Station #2211 #2212";
echo trim(substr($variable, 0, strrpos($variable, "#"))); //8233 Station #2211

How to remove anything in a string after -?

No need for regex. You can use explode:

$str = array_shift(explode('-', $str));

or substr and strpos:

$str = substr($str, 0, strpos($str, '-'));

Maybe in combination with trim to remove leading and trailing whitespaces.

Update: As @Mark points out this will fail if the part you want to get contains a -. It all depends on your possible input.

So assuming you want to remove everything after the last dash, you can use strrpos, which finds the last occurrence of a substring:

$str = substr($str, 0, strrpos($str, '-'));

So you see, there is no regular expression needed ;)

Remove everything BEFORE and AFTER certain chars in a string

Try something like this:

// vars
$string = ' This is text before <html> and this is the text in between </html> And after there is some more text.. ';
$first = '<html>';
$last = '</html>';
// job
$start = stripos($string, $first); // first occurrence position
$length = strripos($string, $last); // last occurrence position
$newString = substr($string, $start, ($length-$start+strlen($last)));
// output
var_dump($string,$start,$length,$newString);

How to get everything after a certain character?

The strpos() finds the offset of the underscore, then substr grabs everything from that index plus 1, onwards.

$data = "123_String";    
$whatIWant = substr($data, strpos($data, "_") + 1);
echo $whatIWant;

If you also want to check if the underscore character (_) exists in your string before trying to get it, you can use the following:

if (($pos = strpos($data, "_")) !== FALSE) { 
$whatIWant = substr($data, $pos+1);
}

Remove everything up to and including character in PHP string

This can be achieved using string manipulation functions in PHP. First we find the position of the - character in the string using strpos(). Use substr() to get everything until that character (including that one). Then use trim() to remove whitespace from the beginning and/or end of the string:

echo trim(substr($str, strpos($str, '-') + 1)); // => £20.00

Alternatively, you could split the string into two pieces, with - as the delimiter, and take the second part:

echo trim(explode('-', $str)[1]);

This could be done in many different ways. In the end, it all boils down to your preferences and requirements.

PHP remove characters after a specific string for a certain length

Not always needed, but a regex will do it:

$string = preg_replace('/\?[\d]{13}/', '', $string);

If the timestamp is not always 13 digits then replace the {13} with just a +.

Remove everything from the first occurrence of a character to the end of a string in PHP

Here's one way:

$print = preg_replace('/^([^,]*).*$/', '$1', $print);

Another:

list($print) = explode(',', $print);

Or:

$print = explode(',', $print)[0];


Related Topics



Leave a reply



Submit