1
$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.

4
  • Will the $ always be the first character in the string? Commented Dec 28, 2012 at 6:48
  • @Jan yes $ will always in beginning of string.. Commented Dec 28, 2012 at 6:51
  • In the string, or at the beginning of the string? Commented Dec 28, 2012 at 6:51
  • And no one ever think of poor fscanf.. Commented Feb 13, 2013 at 8:12

4 Answers 4

3
$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.)

Sign up to request clarification or add additional context in comments.

Comments

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

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

Comments

1

You can use explode.

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

2 Comments

@MarkMartin: If my asnwer has worked, then can you please accept it as answer?
@MarkMartin: I dont understand the reason for un-accepting the answer. Can you kindly mention the reason in comment. Not complaining, just curious to improve my answer in accordance with the problem you have faced.
0

Using preg_replace:

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

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

1 Comment

Hello, Sorry to say but in some cases there is no $ sign in string...so when i apply this logic in some cases "This" is removed from string. In some cases string will be $string = "This is my string";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.