Remove the string in php

How can I remove part of a string?

Example string: "REGISTER 11223344 here"

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

Remove the string in php

asked Feb 3, 2010 at 13:31

6

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

// str_replace($search, $replace, $subject)
echo str_replace("11223344", "","REGISTER 11223344 here");

Remove the string in php

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:

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().

Remove the string in php

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:

$string="REGISTER 11223344 here";
$s = explode(" ", $string);
unset($s[1]);
$s = implode(" ", $s);
print "$s\n";

Remove the string in php

answered Feb 3, 2010 at 13:33

ghostdog74ghostdog74

313k55 gold badges252 silver badges339 bronze badges

0

str_replace(find, replace, string, count)
  • find Required. Specifies the value to find
  • replace Required. Specifies the value to replace the value in find
  • string Required. Specifies the string to be searched
  • count Optional. A variable that counts the number of replacements

As per OP example:

$Example_string = "REGISTER 11223344 here";
$Example_string_PART_REMOVED = str_replace('11223344', '', $Example_string);

// will leave you with "REGISTER  here"

// finally - clean up potential double spaces, beginning spaces or end spaces that may have resulted from removing the unwanted string
$Example_string_COMPLETED = trim(str_replace('  ', ' ', $Example_string_PART_REMOVED));
// trim() will remove any potential leading and trailing spaces - the additional 'str_replace()' will remove any potential double spaces

// will leave you with "REGISTER here"

answered Nov 8, 2018 at 5:02

1

When you need rule-based matching, you need to use a regular expression:

$string = "REGISTER 11223344 here";
preg_match("/(\d+)/", $string, $match);
$number = $match[1];

That will match the first set of numbers, so if you need to be more specific, try:

$string = "REGISTER 11223344 here";
preg_match("/REGISTER (\d+) here/", $string, $match);
$number = $match[1];

Remove the string in php

answered Feb 3, 2010 at 15:52

Remove the string in php

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.

Remove the string in php

answered Feb 3, 2010 at 13:42

Remove the string in php

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:

/**
 * Removes index/indexes from a string, using a delimiter.
 * 
 * @param string $string
 * @param int|int[] $index An index, or a list of indexes to be removed from string.
 * @param string $delimiter 
 * @return string
 * @todo Note: For PHP versions lower than 7.0, remove scalar type hints (i.e. the
 * types before each argument) and the return type.
 */
function removeFromString(string $string, $index, string $delimiter = " "): string
{
    $stringParts = explode($delimiter, $string);

    // Remove indexes from string parts
    if (is_array($index)) {
        foreach ($index as $i) {
            unset($stringParts[(int)($i)]);
        }
    } else {
        unset($stringParts[(int)($index)]);
    }

    // Join all parts together and return it
    return implode($delimiter, $stringParts);
}

For your purpose:

remove_from_str("REGISTER 11223344 here", 1); // Output: REGISTER here

One of its usages is to execute command-like strings, which you know their structures.

answered Mar 2, 2018 at 21:32

Remove the string in php

MAChitgarhaMAChitgarha

3,0292 gold badges28 silver badges36 bronze badges

1

The following snippet will print "REGISTER here"

$string = "REGISTER 11223344 here";

$result = preg_replace(
   array('/(\d+)/'),
   array(''),
   $string
);

print_r($result);

The preg_replace() API usage is as given below.

$result = preg_replace(
    array('/pattern1/', '/pattern2/'),
    array('replace1', 'replace2'),
    $input_string
);

Remove the string in php

answered Jan 1, 2020 at 15:11

Remove the string in php

arango_86arango_86

4,0144 gold badges37 silver badges45 bronze badges

3

Do the following

$string = 'REGISTER 11223344 here';

$content = preg_replace('/REGISTER(.*)here/','',$string);

This would return "REGISTERhere"

or

$string = 'REGISTER 11223344 here';

$content = preg_replace('/REGISTER (.*) here/','',$string);

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);.