Ereg_replace PHP
The modified string is returned. (Which may mean that the original string is returned if there are no matches to be replaced. ) Show If pattern contains parenthesized substrings, replacement may contain substrings of the form \\digit, which will be replaced by the text matching the digit'th parenthesized substring; \\0 will produce the entire contents of string. Up to nine substrings may be used. Parentheses may be nested, in which case they are counted by the opening parenthesis If no matches are found in string, then string will be returned unchanged For example, the following code snippet prints "This was a test" three times Example 1. ereg_replace() example Input: $original_string = "Geeksforgeeks PHP article."; $string_pattern = "(.*)PHP(.*)"; $replace_string = " You should read \\1all\\2"; Output: You should read Geeksforgeeks all article. Explanation: Within the parenthesis "\1" and "\2" to access the part of string and replace with 'PHP' to 'all'. Input: $original_string = "Geeksforgeeks is no:one computer science portal."; $replace_string = '1'; $original_string = ereg_replace('one', $replace_string, $original_string); Output: Geeksforgeeks is no:1 computer science portal.2 Input: $original_string = "Geeksforgeeks PHP article."; $string_pattern = "(.*)PHP(.*)"; $replace_string = " You should read \\1all\\2"; Output: You should read Geeksforgeeks all article. Explanation: Within the parenthesis "\1" and "\2" to access the part of string and replace with 'PHP' to 'all'. Input: $original_string = "Geeksforgeeks is no:one computer science portal."; $replace_string = '1'; $original_string = ereg_replace('one', $replace_string, $original_string); Output: Geeksforgeeks is no:1 computer science portal.3 One thing to take note of is that if you use an integer value as the replacement parameter, you may not get the results you expect. This is because ereg_replace() will interpret the number as the ordinal value of a character, and apply that. For instance Example 2. ereg_replace() example Input: $original_string = "Geeksforgeeks PHP article."; $string_pattern = "(.*)PHP(.*)"; $replace_string = " You should read \\1all\\2"; Output: You should read Geeksforgeeks all article. Explanation: Within the parenthesis "\1" and "\2" to access the part of string and replace with 'PHP' to 'all'. Input: $original_string = "Geeksforgeeks is no:one computer science portal."; $replace_string = '1'; $original_string = ereg_replace('one', $replace_string, $original_string); Output: Geeksforgeeks is no:1 computer science portal.4 Input: $original_string = "Geeksforgeeks PHP article."; $string_pattern = "(.*)PHP(.*)"; $replace_string = " You should read \\1all\\2"; Output: You should read Geeksforgeeks all article. Explanation: Within the parenthesis "\1" and "\2" to access the part of string and replace with 'PHP' to 'all'. Input: $original_string = "Geeksforgeeks is no:one computer science portal."; $replace_string = '1'; $original_string = ereg_replace('one', $replace_string, $original_string); Output: Geeksforgeeks is no:1 computer science portal.5 Example 3. Replace URLs with links Input: $original_string = "Geeksforgeeks PHP article."; $string_pattern = "(.*)PHP(.*)"; $replace_string = " You should read \\1all\\2"; Output: You should read Geeksforgeeks all article. Explanation: Within the parenthesis "\1" and "\2" to access the part of string and replace with 'PHP' to 'all'. Input: $original_string = "Geeksforgeeks is no:one computer science portal."; $replace_string = '1'; $original_string = ereg_replace('one', $replace_string, $original_string); Output: Geeksforgeeks is no:1 computer science portal.6
ParametersInput: $original_string = "Geeksforgeeks PHP article."; $string_pattern = "(.*)PHP(.*)"; $replace_string = " You should read \\1all\\2"; Output: You should read Geeksforgeeks all article. Explanation: Within the parenthesis "\1" and "\2" to access the part of string and replace with 'PHP' to 'all'. Input: $original_string = "Geeksforgeeks is no:one computer science portal."; $replace_string = '1'; $original_string = ereg_replace('one', $replace_string, $original_string); Output: Geeksforgeeks is no:1 computer science portal.8 A POSIX extended regular expression Input: $original_string = "Geeksforgeeks PHP article."; $string_pattern = "(.*)PHP(.*)"; $replace_string = " You should read \\1all\\2"; Output: You should read Geeksforgeeks all article. Explanation: Within the parenthesis "\1" and "\2" to access the part of string and replace with 'PHP' to 'all'. Input: $original_string = "Geeksforgeeks is no:one computer science portal."; $replace_string = '1'; $original_string = ereg_replace('one', $replace_string, $original_string); Output: Geeksforgeeks is no:1 computer science portal.9 If Input: $original_string = "Geeksforgeeks PHP article."; $string_pattern = "(.*)PHP(.*)"; $replace_string = " You should read \\1all\\2"; Output: You should read Geeksforgeeks all article. Explanation: Within the parenthesis "\1" and "\2" to access the part of string and replace with 'PHP' to 'all'. Input: $original_string = "Geeksforgeeks is no:one computer science portal."; $replace_string = '1'; $original_string = ereg_replace('one', $replace_string, $original_string); Output: Geeksforgeeks is no:1 computer science portal.8 contains parenthesized substrings, Input: $original_string = "Geeksforgeeks PHP article."; $string_pattern = "(.*)PHP(.*)"; $replace_string = " You should read \\1all\\2"; Output: You should read Geeksforgeeks all article. Explanation: Within the parenthesis "\1" and "\2" to access the part of string and replace with 'PHP' to 'all'. Input: $original_string = "Geeksforgeeks is no:one computer science portal."; $replace_string = '1'; $original_string = ereg_replace('one', $replace_string, $original_string); Output: Geeksforgeeks is no:1 computer science portal.9 may contain substrings of the form \digit, which will be replaced by the text matching the digit'th parenthesized substring; \0 will produce the entire contents of string. Up to nine substrings may be used. Parentheses may be nested, in which case they are counted by the opening parenthesis Input: $original_string = "Geeksforgeeks PHP article."; $string_pattern = "(.*)PHP(.*)"; $replace_string = " You should read \\1all\\2"; Output: You should read Geeksforgeeks all article. Explanation: Within the parenthesis "\1" and "\2" to access the part of string and replace with 'PHP' to 'all'. Input: $original_string = "Geeksforgeeks is no:one computer science portal."; $replace_string = '1'; $original_string = ereg_replace('one', $replace_string, $original_string); Output: Geeksforgeeks is no:1 computer science portal.7 The input string Return ValuesThe modified string is returned. If no matches are found in Input: $original_string = "Geeksforgeeks PHP article."; $string_pattern = "(.*)PHP(.*)"; $replace_string = " You should read \\1all\\2"; Output: You should read Geeksforgeeks all article. Explanation: Within the parenthesis "\1" and "\2" to access the part of string and replace with 'PHP' to 'all'. Input: $original_string = "Geeksforgeeks is no:one computer science portal."; $replace_string = '1'; $original_string = ereg_replace('one', $replace_string, $original_string); Output: Geeksforgeeks is no:1 computer science portal.7, then it will be returned unchanged ExamplesFor example, the following code snippet prints "This was a test" three times Example #1 ereg_replace() example Input: $original_string = "Geeksforgeeks PHP article."; $string_pattern = "(.*)PHP(.*)"; $replace_string = " You should read \\1all\\2"; Output: You should read Geeksforgeeks all article. Explanation: Within the parenthesis "\1" and "\2" to access the part of string and replace with 'PHP' to 'all'. Input: $original_string = "Geeksforgeeks is no:one computer science portal."; $replace_string = '1'; $original_string = ereg_replace('one', $replace_string, $original_string); Output: Geeksforgeeks is no:1 computer science portal.62 Input: $original_string = "Geeksforgeeks PHP article."; $string_pattern = "(.*)PHP(.*)"; $replace_string = " You should read \\1all\\2"; Output: You should read Geeksforgeeks all article. Explanation: Within the parenthesis "\1" and "\2" to access the part of string and replace with 'PHP' to 'all'. Input: $original_string = "Geeksforgeeks is no:one computer science portal."; $replace_string = '1'; $original_string = ereg_replace('one', $replace_string, $original_string); Output: Geeksforgeeks is no:1 computer science portal.63 One thing to take note of is that if you use an integer value as the Input: $original_string = "Geeksforgeeks PHP article."; $string_pattern = "(.*)PHP(.*)"; $replace_string = " You should read \\1all\\2"; Output: You should read Geeksforgeeks all article. Explanation: Within the parenthesis "\1" and "\2" to access the part of string and replace with 'PHP' to 'all'. Input: $original_string = "Geeksforgeeks is no:one computer science portal."; $replace_string = '1'; $original_string = ereg_replace('one', $replace_string, $original_string); Output: Geeksforgeeks is no:1 computer science portal.9 parameter, you may not get the results you expect. This is because ereg_replace() will interpret the number as the ordinal value of a character, and apply that. For instance The ereg_replace() function searches for string specified by pattern and replaces pattern with replacement if found. The ereg_replace() function operates under the same premises as ereg(), except that the functionality is extended to finding and replacing pattern instead of simply locating it Input: $original_string = "Geeksforgeeks PHP article."; $string_pattern = "(.*)PHP(.*)"; $replace_string = " You should read \\1all\\2"; Output: You should read Geeksforgeeks all article. Explanation: Within the parenthesis "\1" and "\2" to access the part of string and replace with 'PHP' to 'all'. Input: $original_string = "Geeksforgeeks is no:one computer science portal."; $replace_string = '1'; $original_string = ereg_replace('one', $replace_string, $original_string); Output: Geeksforgeeks is no:1 computer science portal.9 The ereg_replace() is an inbuilt function in PHP and is used to search a string pattern in an other string. If pattern is found in the original string then it will replace matching text with a replacement string. You may refer to the article on Regular Expression for basic understanding of pattern matching using regular expressions cú pháp string ereg_replace ( $string_pattern, $replace_string, $original_string ) Parameters Used. This function accepts three mandatory parameters and all of these parameters are described below
Return Value. This function returns a modified string or array if matches found. If matches not fount in the original string then it will return unchanged original string or array Ghi chú. The ereg_replace() function is case sensitive in PHP. This function was deprecated in PHP 5. 3. 0, and removed in PHP 7. 0. 0 Examples Input: $original_string = "Geeksforgeeks PHP article."; $string_pattern = "(.*)PHP(.*)"; $replace_string = " You should read \\1all\\2"; Output: You should read Geeksforgeeks all article. Explanation: Within the parenthesis "\1" and "\2" to access the part of string and replace with 'PHP' to 'all'. Input: $original_string = "Geeksforgeeks is no:one computer science portal."; $replace_string = '1'; $original_string = ereg_replace('one', $replace_string, $original_string); Output: Geeksforgeeks is no:1 computer science portal. Below programs illustrate the ereg_replace() function Program 1 Input: $original_string = "Geeksforgeeks PHP article."; $string_pattern = "(.*)PHP(.*)"; $replace_string = " You should read \\1all\\2"; Output: You should read Geeksforgeeks all article. Explanation: Within the parenthesis "\1" and "\2" to access the part of string and replace with 'PHP' to 'all'. Input: $original_string = "Geeksforgeeks is no:one computer science portal."; $replace_string = '1'; $original_string = ereg_replace('one', $replace_string, $original_string); Output: Geeksforgeeks is no:1 computer science portal.0 Input: $original_string = "Geeksforgeeks PHP article."; $string_pattern = "(.*)PHP(.*)"; $replace_string = " You should read \\1all\\2"; Output: You should read Geeksforgeeks all article. Explanation: Within the parenthesis "\1" and "\2" to access the part of string and replace with 'PHP' to 'all'. Input: $original_string = "Geeksforgeeks is no:one computer science portal."; $replace_string = '1'; $original_string = ereg_replace('one', $replace_string, $original_string); Output: Geeksforgeeks is no:1 computer science portal.1 Input: $original_string = "Geeksforgeeks PHP article."; $string_pattern = "(.*)PHP(.*)"; $replace_string = " You should read \\1all\\2"; Output: You should read Geeksforgeeks all article. Explanation: Within the parenthesis "\1" and "\2" to access the part of string and replace with 'PHP' to 'all'. Input: $original_string = "Geeksforgeeks is no:one computer science portal."; $replace_string = '1'; $original_string = ereg_replace('one', $replace_string, $original_string); Output: Geeksforgeeks is no:1 computer science portal.2 Input: $original_string = "Geeksforgeeks PHP article."; $string_pattern = "(.*)PHP(.*)"; $replace_string = " You should read \\1all\\2"; Output: You should read Geeksforgeeks all article. Explanation: Within the parenthesis "\1" and "\2" to access the part of string and replace with 'PHP' to 'all'. Input: $original_string = "Geeksforgeeks is no:one computer science portal."; $replace_string = '1'; $original_string = ereg_replace('one', $replace_string, $original_string); Output: Geeksforgeeks is no:1 computer science portal.3 Input: $original_string = "Geeksforgeeks PHP article."; $string_pattern = "(.*)PHP(.*)"; $replace_string = " You should read \\1all\\2"; Output: You should read Geeksforgeeks all article. Explanation: Within the parenthesis "\1" and "\2" to access the part of string and replace with 'PHP' to 'all'. Input: $original_string = "Geeksforgeeks is no:one computer science portal."; $replace_string = '1'; $original_string = ereg_replace('one', $replace_string, $original_string); Output: Geeksforgeeks is no:1 computer science portal.4 Input: $original_string = "Geeksforgeeks PHP article."; $string_pattern = "(.*)PHP(.*)"; $replace_string = " You should read \\1all\\2"; Output: You should read Geeksforgeeks all article. Explanation: Within the parenthesis "\1" and "\2" to access the part of string and replace with 'PHP' to 'all'. Input: $original_string = "Geeksforgeeks is no:one computer science portal."; $replace_string = '1'; $original_string = ereg_replace('one', $replace_string, $original_string); Output: Geeksforgeeks is no:1 computer science portal.5 Input: $original_string = "Geeksforgeeks PHP article."; $string_pattern = "(.*)PHP(.*)"; $replace_string = " You should read \\1all\\2"; Output: You should read Geeksforgeeks all article. Explanation: Within the parenthesis "\1" and "\2" to access the part of string and replace with 'PHP' to 'all'. Input: $original_string = "Geeksforgeeks is no:one computer science portal."; $replace_string = '1'; $original_string = ereg_replace('one', $replace_string, $original_string); Output: Geeksforgeeks is no:1 computer science portal.0 Input: $original_string = "Geeksforgeeks PHP article."; $string_pattern = "(.*)PHP(.*)"; $replace_string = " You should read \\1all\\2"; Output: You should read Geeksforgeeks all article. Explanation: Within the parenthesis "\1" and "\2" to access the part of string and replace with 'PHP' to 'all'. Input: $original_string = "Geeksforgeeks is no:one computer science portal."; $replace_string = '1'; $original_string = ereg_replace('one', $replace_string, $original_string); Output: Geeksforgeeks is no:1 computer science portal.1 Input: $original_string = "Geeksforgeeks PHP article."; $string_pattern = "(.*)PHP(.*)"; $replace_string = " You should read \\1all\\2"; Output: You should read Geeksforgeeks all article. Explanation: Within the parenthesis "\1" and "\2" to access the part of string and replace with 'PHP' to 'all'. Input: $original_string = "Geeksforgeeks is no:one computer science portal."; $replace_string = '1'; $original_string = ereg_replace('one', $replace_string, $original_string); Output: Geeksforgeeks is no:1 computer science portal.2 Input: $original_string = "Geeksforgeeks PHP article."; $string_pattern = "(.*)PHP(.*)"; $replace_string = " You should read \\1all\\2"; Output: You should read Geeksforgeeks all article. Explanation: Within the parenthesis "\1" and "\2" to access the part of string and replace with 'PHP' to 'all'. Input: $original_string = "Geeksforgeeks is no:one computer science portal."; $replace_string = '1'; $original_string = ereg_replace('one', $replace_string, $original_string); Output: Geeksforgeeks is no:1 computer science portal.3 Input: $original_string = "Geeksforgeeks PHP article."; $string_pattern = "(.*)PHP(.*)"; $replace_string = " You should read \\1all\\2"; Output: You should read Geeksforgeeks all article. Explanation: Within the parenthesis "\1" and "\2" to access the part of string and replace with 'PHP' to 'all'. Input: $original_string = "Geeksforgeeks is no:one computer science portal."; $replace_string = '1'; $original_string = ereg_replace('one', $replace_string, $original_string); Output: Geeksforgeeks is no:1 computer science portal.4 Input: $original_string = "Geeksforgeeks PHP article."; $string_pattern = "(.*)PHP(.*)"; $replace_string = " You should read \\1all\\2"; Output: You should read Geeksforgeeks all article. Explanation: Within the parenthesis "\1" and "\2" to access the part of string and replace with 'PHP' to 'all'. Input: $original_string = "Geeksforgeeks is no:one computer science portal."; $replace_string = '1'; $original_string = ereg_replace('one', $replace_string, $original_string); Output: Geeksforgeeks is no:1 computer science portal.5 Input: $original_string = "Geeksforgeeks PHP article."; $string_pattern = "(.*)PHP(.*)"; $replace_string = " You should read \\1all\\2"; Output: You should read Geeksforgeeks all article. Explanation: Within the parenthesis "\1" and "\2" to access the part of string and replace with 'PHP' to 'all'. Input: $original_string = "Geeksforgeeks is no:one computer science portal."; $replace_string = '1'; $original_string = ereg_replace('one', $replace_string, $original_string); Output: Geeksforgeeks is no:1 computer science portal.6 Input: $original_string = "Geeksforgeeks PHP article."; $string_pattern = "(.*)PHP(.*)"; $replace_string = " You should read \\1all\\2"; Output: You should read Geeksforgeeks all article. Explanation: Within the parenthesis "\1" and "\2" to access the part of string and replace with 'PHP' to 'all'. Input: $original_string = "Geeksforgeeks is no:one computer science portal."; $replace_string = '1'; $original_string = ereg_replace('one', $replace_string, $original_string); Output: Geeksforgeeks is no:1 computer science portal.1 Input: $original_string = "Geeksforgeeks PHP article."; $string_pattern = "(.*)PHP(.*)"; $replace_string = " You should read \\1all\\2"; Output: You should read Geeksforgeeks all article. Explanation: Within the parenthesis "\1" and "\2" to access the part of string and replace with 'PHP' to 'all'. Input: $original_string = "Geeksforgeeks is no:one computer science portal."; $replace_string = '1'; $original_string = ereg_replace('one', $replace_string, $original_string); Output: Geeksforgeeks is no:1 computer science portal.8 Input: $original_string = "Geeksforgeeks PHP article."; $string_pattern = "(.*)PHP(.*)"; $replace_string = " You should read \\1all\\2"; Output: You should read Geeksforgeeks all article. Explanation: Within the parenthesis "\1" and "\2" to access the part of string and replace with 'PHP' to 'all'. Input: $original_string = "Geeksforgeeks is no:one computer science portal."; $replace_string = '1'; $original_string = ereg_replace('one', $replace_string, $original_string); Output: Geeksforgeeks is no:1 computer science portal.9 Input: $original_string = "Geeksforgeeks PHP article."; $string_pattern = "(.*)PHP(.*)"; $replace_string = " You should read \\1all\\2"; Output: You should read Geeksforgeeks all article. Explanation: Within the parenthesis "\1" and "\2" to access the part of string and replace with 'PHP' to 'all'. Input: $original_string = "Geeksforgeeks is no:one computer science portal."; $replace_string = '1'; $original_string = ereg_replace('one', $replace_string, $original_string); Output: Geeksforgeeks is no:1 computer science portal.4 Write own yours own biography topic.1 Input: $original_string = "Geeksforgeeks PHP article."; $string_pattern = "(.*)PHP(.*)"; $replace_string = " You should read \\1all\\2"; Output: You should read Geeksforgeeks all article. Explanation: Within the parenthesis "\1" and "\2" to access the part of string and replace with 'PHP' to 'all'. Input: $original_string = "Geeksforgeeks is no:one computer science portal."; $replace_string = '1'; $original_string = ereg_replace('one', $replace_string, $original_string); Output: Geeksforgeeks is no:1 computer science portal.6 Input: $original_string = "Geeksforgeeks PHP article."; $string_pattern = "(.*)PHP(.*)"; $replace_string = " You should read \\1all\\2"; Output: You should read Geeksforgeeks all article. Explanation: Within the parenthesis "\1" and "\2" to access the part of string and replace with 'PHP' to 'all'. Input: $original_string = "Geeksforgeeks is no:one computer science portal."; $replace_string = '1'; $original_string = ereg_replace('one', $replace_string, $original_string); Output: Geeksforgeeks is no:1 computer science portal.1 Write own yours own biography topic.4 Write own yours own biography topic.5 Write own yours own biography topic.6 Write own yours own biography topic.7 Write own yours own biography topic.8 Write own yours own biography topic.9 Write own yours own biography topic.8 Input: $original_string = "Geeksforgeeks PHP article."; $string_pattern = "(.*)PHP(.*)"; $replace_string = " You should read \\1all\\2"; Output: You should read Geeksforgeeks all article. Explanation: Within the parenthesis "\1" and "\2" to access the part of string and replace with 'PHP' to 'all'. Input: $original_string = "Geeksforgeeks is no:one computer science portal."; $replace_string = '1'; $original_string = ereg_replace('one', $replace_string, $original_string); Output: Geeksforgeeks is no:1 computer science portal.81 Input: $original_string = "Geeksforgeeks PHP article."; $string_pattern = "(.*)PHP(.*)"; $replace_string = " You should read \\1all\\2"; Output: You should read Geeksforgeeks all article. Explanation: Within the parenthesis "\1" and "\2" to access the part of string and replace with 'PHP' to 'all'. Input: $original_string = "Geeksforgeeks is no:one computer science portal."; $replace_string = '1'; $original_string = ereg_replace('one', $replace_string, $original_string); Output: Geeksforgeeks is no:1 computer science portal.82 Input: $original_string = "Geeksforgeeks PHP article."; $string_pattern = "(.*)PHP(.*)"; $replace_string = " You should read \\1all\\2"; Output: You should read Geeksforgeeks all article. Explanation: Within the parenthesis "\1" and "\2" to access the part of string and replace with 'PHP' to 'all'. Input: $original_string = "Geeksforgeeks is no:one computer science portal."; $replace_string = '1'; $original_string = ereg_replace('one', $replace_string, $original_string); Output: Geeksforgeeks is no:1 computer science portal.1 Input: $original_string = "Geeksforgeeks PHP article."; $string_pattern = "(.*)PHP(.*)"; $replace_string = " You should read \\1all\\2"; Output: You should read Geeksforgeeks all article. Explanation: Within the parenthesis "\1" and "\2" to access the part of string and replace with 'PHP' to 'all'. Input: $original_string = "Geeksforgeeks is no:one computer science portal."; $replace_string = '1'; $original_string = ereg_replace('one', $replace_string, $original_string); Output: Geeksforgeeks is no:1 computer science portal.84 đầu ra Write own yours own biography topic. Ghi chú. While using an integer value as the replacement parameter, we do not get expected result as the function interpret the number to ordinal value of character What is Ereg_replace?The ereg_replace() is an inbuilt function in PHP and is used to search a string pattern in an other string . If pattern is found in the original string then it will replace matching text with a replacement string.
What is the difference between Ereg_replace () and Eregi_replace ()?The eregi_replace() function operates exactly like ereg_replace(), except that the search for pattern in string is not case sensitive .
What is ereg function in PHP?The Ereg() function in PHP searches a string to match the regular expression given in the pattern . The function is case-sensitive. |