remove all text from string after given word in php [duplicate] - php

This question already has answers here:
How to remove anything in a string after "-"?
(7 answers)
Closed 7 years ago.
i've a query string like this:
SELECT * FROM `backplanechanneldecoder` WHERE Pair = '3' LIMIT 25,25
and i want to remove all the text which comes after LIMIT.
Note: This string is generated dynamically from another code on which I've no control that's why I've to remove part of string from my end.

Use str_pos to find the word LIMIT and then strip after that using substr
$string = 'SELECT * FROM `backplanechanneldecoder` WHERE Pair = 3 LIMIT 25,25';
$limit_pos = strpos($string, 'LIMIT');
$string = substr($string, 0, $limit_pos);
echo $string;

Related

How to remove all words from a string except the last one with PHP? [duplicate]

This question already has answers here:
Get the last word of a string
(10 answers)
How to get the last element of an array without deleting it?
(33 answers)
Closed 1 year ago.
How can I remove all words from a string except the last one with PHP?
For example the string "Organic Black Olives" would become "Olives".
Try this:
$str = "Organic Black Olives";
// get words list
$words = explode(' ', $str);
// get last word
$last_word = $words[count($words)-1];
echo $last_word;

rtrim function cuts to many characters [duplicate]

This question already has answers here:
PHP rtrim ".php"
(3 answers)
Closed 4 years ago.
Why:
$string = 'foofoxoa';
$toCut = 'xoa';
rtrim($string, $toCut);
return me:
foof
instead of:
foofo
? I don't want to use str_replace or substr for this, because $string and $cut can have different values.
In addition, $cut can be twice in $string, and I want to cut only from the end.
It is because o is in the list of characters to remove from the end.
I think rtrim works its way from end to start and once it reaches a character not in the list it stops.
Example:
php > $string = 'foofolxoa';
php > $toCut = 'xoa';
php > echo rtrim($string, $toCut);
foofol
https://secure.php.net/manual/en/function.rtrim.php#35108

How to get the last 4 characters in a string - Laravel [duplicate]

This question already has answers here:
How can I get the last 7 characters of a PHP string?
(8 answers)
Closed 5 years ago.
I have this string in my database like 12-12-2067. I want to only display the last 4 words in the string 2067 in my view. How do i achieve this ?
$get_string = Item::all()->first();
<p>{{get_string}}</p>
You can use:
substr ($getstring->string, -4)
Because of (-) it will start form the end of the string and it will take the next 4 characters.
Take care that first() it will return an object and not the string you want.
Please read the manual substr.
$str = substr($str, -4);
string substr ( string $string , int $start [, int $length ] )
$dynamicstring = "12-07-2017";
$newstring = substr($dynamicstring, -4);

PHP preg_match_all to extract number with or without decimal [duplicate]

This question already has answers here:
Extract floating point numbers from a delimited string in PHP
(6 answers)
Closed 6 years ago.
I am trying to extract any number out from a string (with or without decimal) and dump that into an array. Below is my code
$matches = array();
$str = "I have string of 21.11 out of 30";
preg_match_all("/\d+/",$str,$matches);
echo var_dump($matches);
The current output has 3 elements below:
21
11
30
But I expect to output only 2 elements below:
21.11
30
How do I change my regex?
You should use this regex (add dot in character class) :
$matches = array();
$str = "I have string of 21.11 out of 30";
preg_match_all("/[\d\.]+/",$str,$matches);
var_dump($matches);
Test it (Ctrl + Enter)

How to Remove a specific character from the end of the string in php or regex [duplicate]

This question already has answers here:
Removing the last character of a string IF it is $variable [duplicate]
(5 answers)
Closed 12 months ago.
I am generating a string dynamically. For example The string looks like this
$string = "this-is-a-test-string-for-example-";
It can also be like this
$string = "this-is-a-test-string-for-example";
I want if there is a hyphen "-" at the end of the string, It should be removed. How can I do that in php or regex?
http://pl1.php.net/trim - that function gets characters to trim as last parameter
trim($string,"-");
or as suggested for right side only
rtrim($string,"-");
If it always at the end (right) of the string this will work
$string = rtrim($string,"-");
$cleanedString = preg_replace('/^(this-is-a-test-string-for-example)-$/', '$1', $string);
$delete = array('-');
if(in_array($string[(strlen($string)-1)], $delete))
$string = substr($string, 0, strlen($string)-1);
You can add other characters to delete into $delete array.

Categories