Mark Martin
Mark Martin

Reputation: 151

Php - String Search

$string = '$12.50 - This is my string';

This is my string & I want remove section from $ to first space using php string function. I have tried substr but it returns me string after $.

Output should be as below:

- This is my string

In some cases there is no $ sign ain first place of string so in those cases "This" is removed from string.

Upvotes: 1

Views: 311

Answers (4)

Bhavik Shah
Bhavik Shah

Reputation: 2291

You can use explode.

$string = '$12.50 - This is my string';
$test = explode(" ", $string, 2);
echo $test[1];

Upvotes: 1

Benz
Benz

Reputation: 2335

Using preg_replace:

$string = '$12.50 - This is my string';

echo preg_replace('/^(.*?)\s/', '', $string);

Upvotes: 0

Kai
Kai

Reputation: 342

$string = '$12.50 - This is my string';
$string = strstr($string, ' ');

http://php.net/manual/en/function.strstr.php

Upvotes: 2

Ben
Ben

Reputation: 57318

$sub = substr($string, strpos($string, " "));

You may need to use strpos($string, " ") + 1 depending on if you want that extra space.

(strpos finds the first occurence of a character.)

Upvotes: 3

Related Questions