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];
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.
PHP Remove the last part of the string, determined by first character occurrence, starting from the end of the string
try this
$string = 'foo-bar-baz-132 another-55-string-961370';
$result = substr ($string , 0, strrpos($string, '-'));
echo $result;
Source
- https://www.php.net/manual/en/function.substr.php
- https://www.php.net/manual/en/function.strrpos.php
How to remove everything after the first occurrence of a number in a string?
The reason for this is that the first capturing group contains [0-9]*
and are thus re-inserted into the result with the $1
backreference.
The simplest regex for this will be
preg_replace('~\d.*~', '', $s)
See the regex demo
The \d.*
regex finds the first digit with \d
(because the string is processed from left to right) and .*
will match up to the end of the line (to match across linebreaks, add s
modifier: '~\d.*~s'
).
PHP demo:
$str = "banner400x300\nicon80x42\nbla bla55x44";
$result = preg_replace('~\d.*~', '', $str);
echo $result;
How can I remove three characters at the end of a string in PHP?
Just do:
echo substr($string, 0, -3);
You don't need to use a strlen
call, since, as noted in the substr documentation:
If length is given and is negative, then that many characters will be omitted from the end of string
A simpler way to remove everything before and including the first occurrence of character?
You could use a combination of stristr()
and ltrim()
to avoid generating arrays:
$original_filename = ltrim(stristr($list->file, '_'), '_');
See the manual: stristr
returns all of haystack starting from and including the first occurrence of needle to the end.
How to remove everything before the first specific character in a string?
I wouldn't recommend using explode, as it causes more issues if there is more than one comma.
// removes everything before the first ,
$new_str = substr($str, ($pos = strpos($str, ',')) !== false ? $pos + 1 : 0);
Edit:
if(($pos = strpos($str, ',')) !== false)
{
$new_str = substr($str, $pos + 1);
}
else
{
$new_str = get_last_word($str);
}
Remove a string from the beginning of a string
Plain form, without regex:
$prefix = 'bla_';
$str = 'bla_string_bla_bla_bla';
if (substr($str, 0, strlen($prefix)) == $prefix) {
$str = substr($str, strlen($prefix));
}
Takes: 0.0369 ms (0.000,036,954 seconds)
And with:
$prefix = 'bla_';
$str = 'bla_string_bla_bla_bla';
$str = preg_replace('/^' . preg_quote($prefix, '/') . '/', '', $str);
Takes: 0.1749 ms (0.000,174,999 seconds) the 1st run (compiling), and 0.0510 ms (0.000,051,021 seconds) after.
Profiled on my server, obviously.
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 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.
Related Topics
Randomly Losing Session Variables Only in Google Chrome & Url Rewriting
How to Use Prepared Statements with Postgres
Work-Around for PHP5's Pdo Rowcount MySQL Issue
PHP & Hash/Fragment Portion of Url
How to Remove Null Values from an Array
Parsing Http_Range Header in PHP
How to Put My Wamp Online for Someone to Access
Colon After Method Declaration
Shell Run/Execute PHP Script with Parameters
Change Cart Total Price in Woocommerce
Which Function in PHP Validate If the String Is Valid HTML
How to Validate Phone Number Using PHP
How to Specify Multiple Return Types on PHP 7
How to Send the Values of an Array of Checkboxes Through Ajax Using Jquery