Remove the string in php
How can I remove part of a string? Show Example string: How can I remove
asked Feb 3, 2010 at 13:31
6 If you're specifically targetting "11223344", then use
LinusGeffarth 25.4k29 gold badges111 silver badges168 bronze badges answered Feb 3, 2010 at 13:33
Dominic RodgerDominic Rodger 95.1k33 gold badges196 silver badges211 bronze badges 0 You can use str_replace(), which is defined as:
So you could write the code as:
If you need better matching via regular expressions you can use preg_replace().
Rahul Gopi 4141 gold badge16 silver badges30 bronze badges answered Feb 3, 2010 at 13:51
dhoratdhorat 1,4291 gold badge8 silver badges4 bronze badges 2 Assuming 11223344 is not constant:
answered Feb 3, 2010 at 13:33
ghostdog74ghostdog74 313k55 gold badges252 silver badges339 bronze badges 0
As per OP example:
answered Nov 8, 2018 at 5:02 1 When you need rule-based matching, you need to use a regular expression:
That will match the first set of numbers, so if you need to be more specific, try:
answered Feb 3, 2010 at 15:52
TravisOTravisO 9,2804 gold badges34 silver badges44 bronze badges 4 substr() is a built-in PHP function which returns part of a string. The function substr() will take a string as input, the index form where you want the string to be trimmed, and an optional parameter is the length of the substring. You can see proper documentation and example code on substr. Note: index for a string starts with 0.
answered Feb 3, 2010 at 13:42
Umair JabbarUmair Jabbar 3,5285 gold badges27 silver badges42 bronze badges 2 Dynamically, if you want to remove (a) part(s) from (a) fixed index(es) of a string, use this function:
For your purpose:
One of its usages is to execute command-like strings, which you know their structures. answered Mar 2, 2018 at 21:32
MAChitgarhaMAChitgarha 3,0292 gold badges28 silver badges36 bronze badges 1 The following snippet will print "REGISTER here"
The preg_replace() API usage is as given below.
answered Jan 1, 2020 at 15:11
arango_86arango_86 4,0144 gold badges37 silver badges45 bronze badges 3 Do the following
This would return "REGISTERhere" or
This would return "REGISTER here" answered Feb 3, 2010 at 13:38
ElitmiarElitmiar 33.2k72 gold badges178 silver badges228 bronze badges 2 Not the answer you're looking for? Browse other questions tagged php string or ask your own question.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.
Why trim () function is used in PHP?Definition and Usage. The trim() function removes whitespace and other predefined characters from both sides of a string. Related functions: ltrim() - Removes whitespace or other predefined characters from the left side of a string.
How do I delete a word in PHP?PHP str_replace() Function. Replace the characters "world" in the string "Hello world!" with "Peter": ... . Using str_replace() with an array and a count variable: $arr = array("blue","red","green","yellow"); ... . Using str_replace() with fewer elements in replace than find: $find = array("Hello","world");. How do I remove a string?How to remove a particular character from a string ?. public class RemoveChar {. public static void main(String[] args) {. String str = "India is my country";. System.out.println(charRemoveAt(str, 7));. public static String charRemoveAt(String str, int p) {. return str.substring(0, p) + str.substring(p + 1);. |