Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remove part of a string in PHP? [closed]

Tags:

string

php

How can I remove part of a string?

Example string: "REGISTER 11223344 here"

How can I remove "11223344" from the above example string?

like image 902
siddharth Avatar asked Feb 03 '10 13:02

siddharth


People also ask

How can I remove part of a string in PHP?

The substr() and strpos() function is used to remove portion of string after certain character. strpos() function: This function is used to find the first occurrence position of a string inside another string. Function returns an integer value of position of first occurrence of string.

How do you delete one part of a string?

We can remove part of the string using REPLACE() function. We can use this function if we know the exact character of the string to remove. REMOVE(): This function replaces all occurrences of a substring within a new substring.

How do you remove portion of a string before a certain character in PHP?

You can use strstr to do this. Show activity on this post. The explode is in fact a better answer, as the question was about removing the text before the string.

How do I remove a word from a string in PHP?

Answer: Use the PHP str_replace() function You can use the PHP str_replace() function to replace all the occurrences of a word within a string.


3 Answers

If you're specifically targetting "11223344", then use str_replace:

// str_replace($search, $replace, $subject)
echo str_replace("11223344", "","REGISTER 11223344 here");
like image 93
Dominic Rodger Avatar answered Sep 23 '22 21:09

Dominic Rodger


You can use str_replace(), which is defined as:

str_replace($search, $replace, $subject)

So you could write the code as:

$subject = 'REGISTER 11223344 here' ;
$search = '11223344' ;
$trimmed = str_replace($search, '', $subject) ;
echo $trimmed ;

If you need better matching via regular expressions you can use preg_replace().

like image 27
dhorat Avatar answered Sep 23 '22 21:09

dhorat


Assuming 11223344 is not constant:

$string="REGISTER 11223344 here";
$s = explode(" ", $string);
unset($s[1]);
$s = implode(" ", $s);
print "$s\n";
like image 33
ghostdog74 Avatar answered Sep 26 '22 21:09

ghostdog74