Reputation: 53
I have a very simple question about php.
I have this string:
This is a simple string | badword is here
And I need this:
The is a simple string
So,
I have used this code below:
$word = substr($word, 0, strpos($word, '|'));
and if I use that code, I have to check is there any |
char in the string, If yes delete it.
So it's very very low speed and I can't use it .
What is the fastest way to get the result, without checking if the |
char is or is not in the main string?
Upvotes: 1
Views: 9576
Reputation: 3868
Best way is to use explode() function.please follow the links below.
**try this example :**
$text= 'This is a simple sting | badword is here';
$var = explode('|', $text);
echo $var[0];
Upvotes: 0
Reputation: 242
You can use explode() to separate string between specific character.
$string = 'This is a simple sting | badword is here';
$var = explode('|', $string);
echo $var[0]; // This is a simple sting
Upvotes: 4
Reputation: 293
you can use explode() function
$string = 'This is a simple sting | badword is here';
$pieces = explode("|", $string );
echo $pieces[0]; // will display This is a simple sting
Upvotes: 2
Reputation: 41885
For this example, alternatively, you could use strtok()
also:
$string = 'This is a simple sting | badword is here';
$result = strtok($string, '|');
echo $result; // This is a simple sting
Upvotes: 10