Change language

PHP Preg_replace () function

Syntax:
preg_replace ($pattern, $replacement, $subject, $limit, $count) 
Parameters: This function takes the five parameters mentioned above and described below.
  • $pattern:this parameter contains a string element that is used to search for content, and can be a string or an array of strings.
  • $replace:A required parameter that specifies a string or array of strings to replace.
  • $subject: a string or array with strings to find and replace.
  • $limit:This parameter specifies the maximum possible replacements for each pattern.
  • $count :This is an optional parameter. This variable will be populated with the number of replacements performed.
Returned value:this function returns an array if the object is an array, or a string otherwise.The following programs illustrate the preg_replace() function in PHP:Program 1: < ? php  
// PHP illustration program
// preg_replace function $string = ’ November 01, 2018’ ; $pattern = ’/ (w + ) (d +), (d +) / i’ ; $replacement = ’${1 } 02, $3’ ;  
// print function output echo preg_replace ( $pattern , $replacement , $string ); ?>
Exit:
November 02, 2018 
Program 2:  
// PHP illustration program
// preg_replace function $subject = array ( ’1’ , ’ GFG’ , ’2’ , ’Geeks’ , ’ 3’ , ’GCET’ , ’ Contribute’ , ’4’ ); $pattern = array ( ’/ d /’ , ’ / [az] / ’ , ’ / [1a] / ’ ); $replace = array ( ’X: $0’ , ’ Y: $0’ , ’Z: $0’ ) ;  
// Print the result returned by the function echo "preg_replace returns" ; print_r (preg_replace ( $pattern , $replace , $subject )); ?>
Exit:
preg_replace returns Array ([0] = > X: Z: 1 [1] = > GFG [2] = > X: 2 [3] = > GY: eY: eY: kY: s [4] = > X : 3 [5] = > GCET [6] = > CY: oY: nY: tY: rY: iY: bY: uY: tY: e [7] = > X: 4) 
Program 3:  
// PHP illustration program
// preg_replace function $count = 0;  
// Show result after replacement and count echo preg_replace ( array ( ’ / d / ’ , ’ / s / ’ ), ’ * ’ , ’Geeks 4 Geeks’ , -1, $count ); echo "" . $count ; ?>
Exit:
Geeks ** * Geeks 3 
Link: http://php.net /manual/en/function.preg-replace.php

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically