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

Asked
Active3 hr before
Viewed126 times

7 Answers

closedremovestring
90%

Below examples use substr() and strpos() function to remove portion of string after certain character.,How to remove portion of a string after a certain character in PHP?,substr() function: The substr() function is an inbuilt function in PHP is used to extract a specific part of string.,Return Value: This function returns an integer value which represents the index of original_str where the string search_str first occurs.

Syntax:

strpos(original_string, search_string, start_pos)

substr(string_name, start_position, string_length_to_cut)
Output:
GeeksForGeeks is the best platform.
GeeksForGeeks
Output:
computer science portal
for geeks
computer science portal
load more v
88%

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., 1 Your question is Unclear/ambiguous. You are using the term remove, but it seems likely that you mean extract so that the targeted string can be stored in the database. – mickmackusa Jan 1 '20 at 21:58 , 2 @mickmackusa remove can be synonymous with take away from, i.e. taking 11223344 out of the string, I fail to see the problem here. At worst it's ambiguous, it's certainly not "vandalized" – Nick Feb 27 '20 at 9:35 , Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers

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

// str_replace($search, $replace, $subject)
echo str_replace("11223344", "", "REGISTER 11223344 here");
load more v
72%

substr_replace() replaces a copy of string delimited by the offset and (optionally) length parameters with the string given in replace. , If offset is negative, the replacing will begin at the offset'th character from the end of string. ,substr_replace — Replace text within a portion of a string,substr() - Return part of a string

A: YYY;
B: YYY;
C: YYY
A: AAA;
B: BBB;
C: CCC
A: AAAXX;
B: BBBX;
C: CCC
65%

< ? php
//Replace the characters "world" in the string "Hello world!" with "Peter":
echo str_replace("world", "Peter", "Hello world!"); ?
>
load more v
75%

Write a PHP script to remove a part of a string from the beginning.,Previous: Write a PHP script to print the next character of a specific character. Next: Write a PHP script to get a hex dump of a string.,Scala Programming Exercises, Practice, Solution, Sample string : '[email protected]'

PHP Code:

< ? php
$sub_string = '[email protected]';
$str = '[email protected]';
if (substr($str, 0, strlen($sub_string)) == $sub_string) {
   $str = substr($str, strlen($sub_string));
}
echo $str.
"\n"; ?
>

Sample Output:

example.com
load more v
40%

ltrim() removes whitespace or other predefined characters from the left side of a string,rtrim() removes whitespace or other predefined characters from the right side of a string,The trim() function removes whitespace and other predefined characters from both sides of a string. There are two variations of this function:,Using substr_replace() to remove the last haracter from a String.

Syntax

substr_replace(string, replacement, start, length)
load more v
22%

Other Related Concepts,$string - Required. This is the string or the variable containing the string from which you want to remove whitespaces or characters.,$charlist - Optional. This parameter specifies the character that needs to be removed from the string. If left empty, all the characters mentioned below would be removed. ,” ” – ordinary white space

Syntax:

trim($string, $charlist)
load more v

Other "closed-remove" queries related to "How can I remove part of a string in PHP? [closed]"