{"id":2713,"date":"2021-09-18T10:20:09","date_gmt":"2021-09-18T10:20:09","guid":{"rendered":"https:\/\/phptutorial.net\/?page_id=2713"},"modified":"2025-04-07T13:24:12","modified_gmt":"2025-04-07T13:24:12","slug":"php-preg_replace","status":"publish","type":"page","link":"https:\/\/www.phptutorial.net\/php-tutorial\/php-preg_replace\/","title":{"rendered":"PHP preg_replace"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use the PHP <code>preg_replace()<\/code> function to search and replace using regular expressions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-php-preg_replace-function'>Introduction to the PHP preg_replace() function <a href=\"#introduction-to-the-php-preg_replace-function\" class=\"anchor\" id=\"introduction-to-the-php-preg_replace-function\" title=\"Anchor for Introduction to the PHP preg_replace() function\">#<\/a><\/h2>\n\n\n\n<p>The <code>preg_replace()<\/code> function searches for matches and replaces them with a pattern.<\/p>\n\n\n\n<p>The following shows how to use the <code>preg_replace()<\/code> function with a single string:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">preg_replace(\n    string $pattern,\n    string $replacement,\n    string $subject,\n    int $limit = <span class=\"hljs-number\">-1<\/span>,\n    int &amp;$count = <span class=\"hljs-keyword\">null<\/span>\n): string<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>preg_replace()<\/code> function has the following parameters:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>$pattern<\/code> is a regular expression to match.<\/li>\n\n\n\n<li><code>$replacement<\/code> is a string to replace. It may contain the backreferences.<\/li>\n\n\n\n<li><code>$subject<\/code> is a string to search and replace.<\/li>\n\n\n\n<li><code>$limit<\/code> is the maximum possible replacement for the pattern. By default, it is -1, which is unlimited.<\/li>\n\n\n\n<li><code>$count<\/code> stores the number of replacements.<\/li>\n<\/ul>\n\n\n\n<p>If the $subject matches the $pattern, the <code>preg_replace()<\/code> function replaces the match with the $replacement and returns it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='php-preg_replace-function-examples'>PHP preg_replace() function examples <a href=\"#php-preg_replace-function-examples\" class=\"anchor\" id=\"php-preg_replace-function-examples\" title=\"Anchor for PHP preg_replace() function examples\">#<\/a><\/h2>\n\n\n\n<p>Let&#8217;s take some examples of using the <code>preg_replace()<\/code> function.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='1-using-the-php-preg_replace-function-to-change-the-date-format'>1) Using the PHP preg_replace() function to change the date format <a href=\"#1-using-the-php-preg_replace-function-to-change-the-date-format\" class=\"anchor\" id=\"1-using-the-php-preg_replace-function-to-change-the-date-format\" title=\"Anchor for 1) Using the PHP preg_replace() function to change the date format\">#<\/a><\/h3>\n\n\n\n<p>The following example uses the <code>preg_replace()<\/code> function to change the date string from <code>dd-mm-yyyy<\/code> to <code>mm-dd-yyyy<\/code> format:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$pattern = <span class=\"hljs-string\">'\/(\\d{2})-(\\d{2})-(\\d{4})\/'<\/span>;\n$replacement = <span class=\"hljs-string\">'\\2-\\1-\\3'<\/span>;\n\n$str = <span class=\"hljs-string\">'25-12-2021'<\/span>;\n\n<span class=\"hljs-keyword\">echo<\/span> preg_replace($pattern, $replacement, $str);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCiRwYXR0ZXJuID0gJy8oXGR7Mn0pLShcZHsyfSktKFxkezR9KS8nOwokcmVwbGFjZW1lbnQgPSAnXDItXDEtXDMnOwoKJHN0ciA9ICcyNS0xMi0yMDIxJzsKCmVjaG8gcHJlZ19yZXBsYWNlKCRwYXR0ZXJuLCAkcmVwbGFjZW1lbnQsICRzdHIpOw\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-number\">12<\/span><span class=\"hljs-number\">-25<\/span><span class=\"hljs-number\">-2021<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<p>The following regular expression matches the date in the <code>dd-mm-yyyy<\/code> format:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">\/(\\d{<span class=\"hljs-number\">2<\/span>})-(\\d{<span class=\"hljs-number\">2<\/span>})-(\\d{<span class=\"hljs-number\">4<\/span>})\/<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>It consists of three capturing groups for day <code>(\\d{2})<\/code>, month <code>(\\d{2})<\/code>, and year <code>(\\d{4})<\/code>.<\/p>\n\n\n\n<p>The replacement string contains three backreferences <code>\\2<\/code> for the second capturing group which is month, <code>\\1<\/code> for the first capturing group which is day, and <code>\\3<\/code> for the third capturing group which is the year.<\/p>\n\n\n\n<p>Therefore, the <code>preg_replace()<\/code> changes the format of the date from <code>dd-mm-yyyy<\/code> to <code>mm-dd-yyyy<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='2-using-the-php-preg_replace-function-to-remove-the-excessive-whitespace'>2) Using the PHP preg_replace() function to remove the excessive whitespace <a href=\"#2-using-the-php-preg_replace-function-to-remove-the-excessive-whitespace\" class=\"anchor\" id=\"2-using-the-php-preg_replace-function-to-remove-the-excessive-whitespace\" title=\"Anchor for 2) Using the PHP preg_replace() function to remove the excessive whitespace\">#<\/a><\/h3>\n\n\n\n<p>The following example uses the <code>preg_replace()<\/code> function to strip excess whitespace from a string:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$str = <span class=\"hljs-string\">'PHP   is    awesome'<\/span>;\n<span class=\"hljs-keyword\">echo<\/span> preg_replace(<span class=\"hljs-string\">'\/\\s\\s+\/'<\/span>, <span class=\"hljs-string\">' '<\/span>, $str);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCiRzdHIgPSAnUEhQICAgaXMgICAgYXdlc29tZSc7CmVjaG8gcHJlZ19yZXBsYWNlKCcvXHNccysvJywgJyAnLCAkc3RyKTs\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">PHP is awesome<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The pattern <code>\/\\s\\s+\/<\/code> matches one or more spaces.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='php-preg_replace-function-with-arrays'>PHP preg_replace() function with arrays <a href=\"#php-preg_replace-function-with-arrays\" class=\"anchor\" id=\"php-preg_replace-function-with-arrays\" title=\"Anchor for PHP preg_replace() function with arrays\">#<\/a><\/h2>\n\n\n\n<p>The <code>preg_replace()<\/code> function also accepts the <code>$pattern<\/code>, <code>$replacement<\/code>, and <code>$subject<\/code> as arrays:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">preg_replace(\n    string|<span class=\"hljs-keyword\">array<\/span> $pattern,\n    string|<span class=\"hljs-keyword\">array<\/span> $replacement,\n    string|<span class=\"hljs-keyword\">array<\/span> $subject,\n    int $limit = <span class=\"hljs-number\">-1<\/span>,\n    int &amp;$count = <span class=\"hljs-keyword\">null<\/span>\n): string|<span class=\"hljs-keyword\">array<\/span>|<span class=\"hljs-keyword\">null<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If both <code>$pattern<\/code> and <code>$replacement<\/code> are arrays and <code>$subject<\/code> is a string, the <code>preg_replace()<\/code> function will replace each pattern in the <code>$pattern<\/code> array with the replacement in the <code>$replacement<\/code> array.<\/p>\n\n\n\n<p>If the <code>$replacement<\/code> array has fewer elements than the <code>$pattern<\/code> array, the <code>preg_replace()<\/code> function wil replace extra patterns with an empty string.<\/p>\n\n\n\n<p>If the <code>$subject<\/code> is an array, the <code>preg_replace()<\/code> function searches and replaces each string in the array and returns the result as an array.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the PHP <code>preg_replace()<\/code> function to search and replace strings using regular expressions.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Did you find this tutorial useful?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"2713\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-preg_replace\/\"\n\t\t\t\tdata-post-title=\"PHP preg_replace\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"2713\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-preg_replace\/\"\n\t\t\t\tdata-post-title=\"PHP preg_replace\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn how to use the PHP preg_replace() function to search and replace using regular expressions.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":15,"menu_order":140,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2713","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/2713","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/comments?post=2713"}],"version-history":[{"count":2,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/2713\/revisions"}],"predecessor-version":[{"id":3268,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/2713\/revisions\/3268"}],"up":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/15"}],"wp:attachment":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/media?parent=2713"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}