{"id":951,"date":"2023-01-07T00:54:23","date_gmt":"2023-01-06T17:54:23","guid":{"rendered":"https:\/\/insideofcode.com\/blog\/?p=951"},"modified":"2023-10-02T22:33:20","modified_gmt":"2023-10-02T15:33:20","slug":"how-to-make-slugify-function","status":"publish","type":"post","link":"https:\/\/insideofcode.com\/blog\/how-to-make-slugify-function\/","title":{"rendered":"How to Make Slugify Function in JavaScript and PHP"},"content":{"rendered":"\n<p>In the realm of web development, &#8220;slugify&#8221; refers to the art of transforming a string, such as a page title or file name, into a URL-friendly format. This process is vital for creating user-friendly URLs that are both easy to comprehend and navigate. Imagine you have a blog post titled &#8220;My Funky Post&#8221;; through slugification, you can convert it into a neat URL like &#8220;my-funky-post&#8221;.<\/p>\n\n\n\n<p>Slugification serves various purposes. Content management systems (CMS) often utilize slugification to automatically generate user-friendly URLs for pages or blog posts. Similarly, in applications like file-sharing platforms, slugification helps create safe and readable file names for uploaded content.<\/p>\n\n\n\n<p>This simple yet powerful technique not only enhances user experience but also boosts your website&#8217;s SEO by creating more accessible URLs.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Slugify in JavaScript:<\/h2>\n\n\n\n<p>Here&#8217;s a JavaScript function that achieves slugification:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"javascript\" class=\"language-javascript\">function slugify(string) {\n  const a = '\u00e0\u00e1\u00e2\u00e4\u00e6\u00e3\u00e5\u0101\u0103\u0105\u00e7\u0107\u010d\u0111\u010f\u00e8\u00e9\u00ea\u00eb\u0113\u0117\u0119\u011b\u011f\u01f5\u1e27\u00ee\u00ef\u00ed\u012b\u012f\u00ec\u0142\u1e3f\u00f1\u0144\u01f9\u0148\u00f4\u00f6\u00f2\u00f3\u0153\u00f8\u014d\u00f5\u0151\u1e55\u0155\u0159\u00df\u015b\u0161\u015f\u0219\u0165\u021b\u00fb\u00fc\u00f9\u00fa\u016b\u01d8\u016f\u0171\u0173\u1e83\u1e8d\u00ff\u00fd\u017e\u017a\u017c\u00b7\/_,:;'\n  const b = 'aaaaaaaaaacccddeeeeeeeegghiiiiiilmnnnnooooooooprrsssssttuuuuuuuuuwxyyzzz------'\n  const p = new RegExp(a.split('').join('|'), 'g')\n\n  return string.toString().toLowerCase()\n    .replace(\/\\s+\/g, '-') \/\/ Replace spaces with -\n    .replace(p, c =&gt; b.charAt(a.indexOf(c))) \/\/ Replace special characters\n    .replace(\/&amp;\/g, '-and-') \/\/ Replace &amp; with 'and'\n    .replace(\/[^\\w-]+\/g, '') \/\/ Remove all non-word characters\n    .replace(\/--+\/g, '-') \/\/ Replace multiple - with single -\n    .replace(\/^-+\/, '') \/\/ Trim - from start of text\n    .replace(\/-+$\/, '') \/\/ Trim - from end of text\n}\n<\/code><\/pre>\n\n\n\n<p>This JavaScript function first converts the string to lowercase, replaces spaces with hyphens, removes special characters, replaces ampersands with &#8220;and&#8221;, handles multiple hyphens, and trims any extra hyphens from the start or end of the string.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Slugify in PHP:<\/h2>\n\n\n\n<p>In PHP, you can achieve slugification using the following function:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">function slugify($string) {\n  $a = '\u00e0\u00e1\u00e2\u00e4\u00e6\u00e3\u00e5\u0101\u0103\u0105\u00e7\u0107\u010d\u0111\u010f\u00e8\u00e9\u00ea\u00eb\u0113\u0117\u0119\u011b\u011f\u01f5\u1e27\u00ee\u00ef\u00ed\u012b\u012f\u00ec\u0142\u1e3f\u00f1\u0144\u01f9\u0148\u00f4\u00f6\u00f2\u00f3\u0153\u00f8\u014d\u00f5\u0151\u1e55\u0155\u0159\u00df\u015b\u0161\u015f\u0219\u0165\u021b\u00fb\u00fc\u00f9\u00fa\u016b\u01d8\u016f\u0171\u0173\u1e83\u1e8d\u00ff\u00fd\u017e\u017a\u017c-\/-,:;';\n  $b = 'aaaaaaaaaacccddeeeeeeeegghiiiiiilmnnnnooooooooprrsssssttuuuuuuuuuwxyyzzz------';\n  $p = '\/[' . preg_quote($a, '\/') . ']\/';\n\n  $string = mb_strtolower($string, 'UTF-8');\n  $string = preg_replace('\/\\s+\/', '-', $string);\n  $string = preg_replace_callback($p, function ($match) use ($a, $b) {\n    return $b[mb_strpos($a, $match[0], 0, 'UTF-8')];\n  }, $string);\n  $string = str_replace('&amp;', '-and-', $string);\n  $string = preg_replace('\/[^\\w-]+\/', '', $string);\n  $string = preg_replace('\/-{2,}\/', '-', $string);\n  $string = trim($string, '-');\n\n  return $string;\n}<\/code><\/pre>\n\n\n\n<p>In PHP, this function performs similar operations as the JavaScript counterpart, ensuring a clean and user-friendly slug.<\/p>\n\n\n\n<p>Implementing these slugify functions in your web applications will contribute to creating readable, SEO-friendly URLs, enhancing user experience, and making your web content more accessible and appealing.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to create SEO-friendly URLs using slugify functions in JavaScript and PHP. Improve user experience and boost website SEO effortlessly.<\/p>\n","protected":false},"author":1,"featured_media":1147,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[139,196,158,3],"tags":[138,137,56],"class_list":{"0":"post-951","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","6":"hentry","7":"category-javascript","9":"category-php","10":"category-programming","11":"tag-helper","12":"tag-javascript","13":"tag-programming"},"blocksy_meta":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to Make Slugify Function in JavaScript and PHP - Inside of Code<\/title>\n<meta name=\"description\" content=\"Learn how to create SEO-friendly URLs using slugify functions in JavaScript and PHP. Improve user experience and boost website SEO easly.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/insideofcode.com\/blog\/how-to-make-slugify-function\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Make Slugify Function in JavaScript and PHP - Inside of Code\" \/>\n<meta property=\"og:description\" content=\"Learn how to create SEO-friendly URLs using slugify functions in JavaScript and PHP. Improve user experience and boost website SEO easly.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/insideofcode.com\/blog\/how-to-make-slugify-function\/\" \/>\n<meta property=\"og:site_name\" content=\"Inside of Code\" \/>\n<meta property=\"article:published_time\" content=\"2023-01-06T17:54:23+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-10-02T15:33:20+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/insideofcode.com\/wp-content\/uploads\/2023\/01\/how-to-make-slugify-function.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"675\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"Dani\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Dani\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/insideofcode.com\/blog\/how-to-make-slugify-function\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/insideofcode.com\/blog\/how-to-make-slugify-function\/\"},\"author\":{\"name\":\"Dani\",\"@id\":\"https:\/\/insideofcode.com\/blog\/#\/schema\/person\/1c6cafc6659067aa7ac1fd1a5ced218d\"},\"headline\":\"How to Make Slugify Function in JavaScript and PHP\",\"datePublished\":\"2023-01-06T17:54:23+00:00\",\"dateModified\":\"2023-10-02T15:33:20+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/insideofcode.com\/blog\/how-to-make-slugify-function\/\"},\"wordCount\":238,\"publisher\":{\"@id\":\"https:\/\/insideofcode.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/insideofcode.com\/blog\/how-to-make-slugify-function\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/insideofcode.com\/wp-content\/uploads\/2023\/01\/how-to-make-slugify-function.webp\",\"keywords\":[\"helper\",\"javascript\",\"programming\"],\"articleSection\":[\"Javascript\",\"Javascript\",\"PHP\",\"Programming\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/insideofcode.com\/blog\/how-to-make-slugify-function\/\",\"url\":\"https:\/\/insideofcode.com\/blog\/how-to-make-slugify-function\/\",\"name\":\"How to Make Slugify Function in JavaScript and PHP - Inside of Code\",\"isPartOf\":{\"@id\":\"https:\/\/insideofcode.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/insideofcode.com\/blog\/how-to-make-slugify-function\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/insideofcode.com\/blog\/how-to-make-slugify-function\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/insideofcode.com\/wp-content\/uploads\/2023\/01\/how-to-make-slugify-function.webp\",\"datePublished\":\"2023-01-06T17:54:23+00:00\",\"dateModified\":\"2023-10-02T15:33:20+00:00\",\"description\":\"Learn how to create SEO-friendly URLs using slugify functions in JavaScript and PHP. Improve user experience and boost website SEO easly.\",\"breadcrumb\":{\"@id\":\"https:\/\/insideofcode.com\/blog\/how-to-make-slugify-function\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/insideofcode.com\/blog\/how-to-make-slugify-function\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/insideofcode.com\/blog\/how-to-make-slugify-function\/#primaryimage\",\"url\":\"https:\/\/insideofcode.com\/wp-content\/uploads\/2023\/01\/how-to-make-slugify-function.webp\",\"contentUrl\":\"https:\/\/insideofcode.com\/wp-content\/uploads\/2023\/01\/how-to-make-slugify-function.webp\",\"width\":1200,\"height\":675,\"caption\":\"How to make slugify function SEO URL\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/insideofcode.com\/blog\/how-to-make-slugify-function\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/insideofcode.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Make Slugify Function in JavaScript and PHP\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/insideofcode.com\/blog\/#website\",\"url\":\"https:\/\/insideofcode.com\/blog\/\",\"name\":\"Inside of Code\",\"description\":\"A software engineer who wants to share what he has learned, and document his journey.\",\"publisher\":{\"@id\":\"https:\/\/insideofcode.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/insideofcode.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/insideofcode.com\/blog\/#organization\",\"name\":\"Inside of Code\",\"url\":\"https:\/\/insideofcode.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/insideofcode.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/insideofcode.com\/wp-content\/uploads\/2022\/10\/logo-ioc.png\",\"contentUrl\":\"https:\/\/insideofcode.com\/wp-content\/uploads\/2022\/10\/logo-ioc.png\",\"width\":324,\"height\":324,\"caption\":\"Inside of Code\"},\"image\":{\"@id\":\"https:\/\/insideofcode.com\/blog\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/insideofcode.com\/blog\/#\/schema\/person\/1c6cafc6659067aa7ac1fd1a5ced218d\",\"name\":\"Dani\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/insideofcode.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/a4b1e4251720747aef0418e09fe0f6e7786ff89358b57f76822f1c52c286552b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/a4b1e4251720747aef0418e09fe0f6e7786ff89358b57f76822f1c52c286552b?s=96&d=mm&r=g\",\"caption\":\"Dani\"},\"sameAs\":[\"https:\/\/insideofcode.com\/blog\"],\"url\":\"https:\/\/insideofcode.com\/blog\/author\/daniwork\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Make Slugify Function in JavaScript and PHP - Inside of Code","description":"Learn how to create SEO-friendly URLs using slugify functions in JavaScript and PHP. Improve user experience and boost website SEO easly.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/insideofcode.com\/blog\/how-to-make-slugify-function\/","og_locale":"en_US","og_type":"article","og_title":"How to Make Slugify Function in JavaScript and PHP - Inside of Code","og_description":"Learn how to create SEO-friendly URLs using slugify functions in JavaScript and PHP. Improve user experience and boost website SEO easly.","og_url":"https:\/\/insideofcode.com\/blog\/how-to-make-slugify-function\/","og_site_name":"Inside of Code","article_published_time":"2023-01-06T17:54:23+00:00","article_modified_time":"2023-10-02T15:33:20+00:00","og_image":[{"width":1200,"height":675,"url":"https:\/\/insideofcode.com\/wp-content\/uploads\/2023\/01\/how-to-make-slugify-function.webp","type":"image\/webp"}],"author":"Dani","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Dani","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/insideofcode.com\/blog\/how-to-make-slugify-function\/#article","isPartOf":{"@id":"https:\/\/insideofcode.com\/blog\/how-to-make-slugify-function\/"},"author":{"name":"Dani","@id":"https:\/\/insideofcode.com\/blog\/#\/schema\/person\/1c6cafc6659067aa7ac1fd1a5ced218d"},"headline":"How to Make Slugify Function in JavaScript and PHP","datePublished":"2023-01-06T17:54:23+00:00","dateModified":"2023-10-02T15:33:20+00:00","mainEntityOfPage":{"@id":"https:\/\/insideofcode.com\/blog\/how-to-make-slugify-function\/"},"wordCount":238,"publisher":{"@id":"https:\/\/insideofcode.com\/blog\/#organization"},"image":{"@id":"https:\/\/insideofcode.com\/blog\/how-to-make-slugify-function\/#primaryimage"},"thumbnailUrl":"https:\/\/insideofcode.com\/wp-content\/uploads\/2023\/01\/how-to-make-slugify-function.webp","keywords":["helper","javascript","programming"],"articleSection":["Javascript","Javascript","PHP","Programming"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/insideofcode.com\/blog\/how-to-make-slugify-function\/","url":"https:\/\/insideofcode.com\/blog\/how-to-make-slugify-function\/","name":"How to Make Slugify Function in JavaScript and PHP - Inside of Code","isPartOf":{"@id":"https:\/\/insideofcode.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/insideofcode.com\/blog\/how-to-make-slugify-function\/#primaryimage"},"image":{"@id":"https:\/\/insideofcode.com\/blog\/how-to-make-slugify-function\/#primaryimage"},"thumbnailUrl":"https:\/\/insideofcode.com\/wp-content\/uploads\/2023\/01\/how-to-make-slugify-function.webp","datePublished":"2023-01-06T17:54:23+00:00","dateModified":"2023-10-02T15:33:20+00:00","description":"Learn how to create SEO-friendly URLs using slugify functions in JavaScript and PHP. Improve user experience and boost website SEO easly.","breadcrumb":{"@id":"https:\/\/insideofcode.com\/blog\/how-to-make-slugify-function\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/insideofcode.com\/blog\/how-to-make-slugify-function\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/insideofcode.com\/blog\/how-to-make-slugify-function\/#primaryimage","url":"https:\/\/insideofcode.com\/wp-content\/uploads\/2023\/01\/how-to-make-slugify-function.webp","contentUrl":"https:\/\/insideofcode.com\/wp-content\/uploads\/2023\/01\/how-to-make-slugify-function.webp","width":1200,"height":675,"caption":"How to make slugify function SEO URL"},{"@type":"BreadcrumbList","@id":"https:\/\/insideofcode.com\/blog\/how-to-make-slugify-function\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/insideofcode.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to Make Slugify Function in JavaScript and PHP"}]},{"@type":"WebSite","@id":"https:\/\/insideofcode.com\/blog\/#website","url":"https:\/\/insideofcode.com\/blog\/","name":"Inside of Code","description":"A software engineer who wants to share what he has learned, and document his journey.","publisher":{"@id":"https:\/\/insideofcode.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/insideofcode.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/insideofcode.com\/blog\/#organization","name":"Inside of Code","url":"https:\/\/insideofcode.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/insideofcode.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/insideofcode.com\/wp-content\/uploads\/2022\/10\/logo-ioc.png","contentUrl":"https:\/\/insideofcode.com\/wp-content\/uploads\/2022\/10\/logo-ioc.png","width":324,"height":324,"caption":"Inside of Code"},"image":{"@id":"https:\/\/insideofcode.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/insideofcode.com\/blog\/#\/schema\/person\/1c6cafc6659067aa7ac1fd1a5ced218d","name":"Dani","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/insideofcode.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/a4b1e4251720747aef0418e09fe0f6e7786ff89358b57f76822f1c52c286552b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/a4b1e4251720747aef0418e09fe0f6e7786ff89358b57f76822f1c52c286552b?s=96&d=mm&r=g","caption":"Dani"},"sameAs":["https:\/\/insideofcode.com\/blog"],"url":"https:\/\/insideofcode.com\/blog\/author\/daniwork\/"}]}},"_links":{"self":[{"href":"https:\/\/insideofcode.com\/blog\/wp-json\/wp\/v2\/posts\/951","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/insideofcode.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/insideofcode.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/insideofcode.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/insideofcode.com\/blog\/wp-json\/wp\/v2\/comments?post=951"}],"version-history":[{"count":0,"href":"https:\/\/insideofcode.com\/blog\/wp-json\/wp\/v2\/posts\/951\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/insideofcode.com\/blog\/wp-json\/wp\/v2\/media\/1147"}],"wp:attachment":[{"href":"https:\/\/insideofcode.com\/blog\/wp-json\/wp\/v2\/media?parent=951"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/insideofcode.com\/blog\/wp-json\/wp\/v2\/categories?post=951"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/insideofcode.com\/blog\/wp-json\/wp\/v2\/tags?post=951"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}