PHP String Function to Get Substring Before the Last Occurrence of a Character

php string function to get substring before the last occurrence of a character

This is kind of a cheap way to do it, but you could split, pop, and then join to get it done:

$string = 'Hello World Again';
$string = explode(' ', $string);
array_pop($string);
$string = implode(' ', $string);

get string before last occurrence of specific character

echo dirname('js/jstree/_lib/jquery.js');

Get the characters before the last - (dash)

You'll want to find the last occurrence of -, using strrpos().

$output = substr( $number, 0, strrpos( $number, '-' ) );

Insert before the last occurrence of a specific character in a string in PHP

One option is to use a regular expression with negative lookahead:

print preg_replace("~<p>(?!.*<p>)~", "Hello $0", $tdr);

If you like string functions more, try strrpos:

 $n = strrpos($tdr, '<p>');
print substr($tdr, 0, $n) . ' Hello ' . substr($tdr, $n);

PHP substring extraction. Get the string before the first '/' or the whole string

Use explode()

$arr = explode("/", $string, 2);
$first = $arr[0];

In this case, I'm using the limit parameter to explode so that php won't scan the string any more than what's needed.

Returning a string after the second occurrence of a character and before the last occurrence of the same character with php

Just use explode(), splitting by . Then, get the second and third elements:

<?php
$description = "ORG-000012 – Name of variation – Quantity: 12 Pack – $14.95";
$descriptionArray = explode(" – ", $description);
$finalDescription = $descriptionArray[1]." – ".$descriptionArray[2];
var_dump($finalDescription); // string(39) "Name of variation – Quantity: 12 Pack"

Demo

Or, if the number of elements in between is variable, array_shift() and array_pop() the array to remove the first and last elements:

<?php
$description = "ORG-000012 – Name of variation – Quantity: 12 Pack – $14.95";
$descriptionArray = explode(" – ", $description);
array_pop($descriptionArray);
array_shift($descriptionArray);
var_dump($descriptionArray);

Demo

How to get the last occurrence of a string?

As mentioned in my comment, swap out strpos() for strrpos()

Find the numeric position of the last occurrence of needle in the haystack string.

Also, I believe you should change

if ($ini == 0)

to

if ($ini === false)

The former will match if $start is at the very beginning of $string. The latter will match if $start cannot be found at all.

Demo ~ https://eval.in/1027283

PHP get everything in a string before underscore

You should simple use:

$imagePreFix = substr($fileinfo['basename'], 0, strpos($fileinfo['basename'], "_"));

I don't see any reason to use explode and create extra array just to get first element.

You can also use (in PHP 5.3+):

$imagePreFix = strstr($fileinfo['basename'], '_', true); 

PHP remove characters after last occurrence of a character in a string

You should use the tool that is designed for this type of job, parse_url

url.php

<?php

$urls = array('http://example.com/foo?u=ben',
'http://example.com/foo/bar/?u=ben',
'http://example.com/foo/bar/baz?u=ben',
'https://foo.example.com/foo/bar/baz?u=ben',
);

function clean_url($url) {
$parts = parse_url($url);
return $parts['scheme'] . '://' . $parts['host'] . $parts['path'];
}

foreach ($urls as $url) {
echo clean_url($url) . "\n";
}

Example:

·> php url.php                                                                                                 
http://example.com/foo
http://example.com/foo/bar/
http://example.com/foo/bar/baz
https://foo.example.com/foo/bar/baz


Related Topics



Leave a reply



Submit