The opposite PHP function strstr to return the first part of a string, not the last

I have a line, something like this:

$abcdef(+$1.00) 

I am trying to get the first part of the line before the first bracket:

 $abcdef 

Currently, if I use strstr() , it will return part of the string after the specified character:

 $newstr = strstr($var, '('); 

I want a piece before it arrives. What is the inverse or opposite strstr () function that will do this?

+6
source share
3 answers

Pass true as the third parameter.

On the page that you indicated :

 string strstr (string $haystack , mixed $needle [, bool $before_needle = false ]) 

before_needle : if TRUE, strstr () returns the portion of the haystack before the first occurrence of the needle (excluding the needle).

Note. This option was added only in PHP 5.3. If for some reason you have an old version, a combination of substr() and strpos() should help:

 $newstr = substr( $var, 0, strpos( $var, '(' ) ); 
+12
source

Set the third parameter strstr to true, it will return the appearance before the needle

+4
source

The best way to do this in a simple way:

$ newstr = explode ('(', $ var) [0];

Explanation: $ result = explode ('search_for_this', $ search_in_this) [0];

0
source

Source: https://habr.com/ru/post/916378/


All Articles