“Slug dalam PHP” Kode Jawaban

string ke slug php

function slugify($text, string $divider = '-')
{
  // replace non letter or digits by divider
  $text = preg_replace('~[^\pL\d]+~u', $divider, $text);

  // transliterate
  $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);

  // remove unwanted characters
  $text = preg_replace('~[^-\w]+~', '', $text);

  // trim
  $text = trim($text, $divider);

  // remove duplicate divider
  $text = preg_replace('~-+~', $divider, $text);

  // lowercase
  $text = strtolower($text);

  if (empty($text)) {
    return 'n-a';
  }

  return $text;
}

echo slugify('Hello World'); //hello-world
echo slugify('Hello World', '_'); //hello_world
Beautiful Bug

PHP dapatkan siput

You can get that using the following methods:

<?php $post_slug = get_post_field( 'post_name', get_post() ); ?>
Or You can use this easy code:

<?php
    global $post;
    $post_slug = $post->post_name;
?>
Santino

Buat Slug dengan PHP

public static function slugify($text, string $divider = '-')
{
  // replace non letter or digits by divider
  $text = preg_replace('~[^\pL\d]+~u', $divider, $text);

  // transliterate
  $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);

  // remove unwanted characters
  $text = preg_replace('~[^-\w]+~', '', $text);

  // trim
  $text = trim($text, $divider);

  // remove duplicate divider
  $text = preg_replace('~-+~', $divider, $text);

  // lowercase
  $text = strtolower($text);

  if (empty($text)) {
    return 'n-a';
  }

  return $text;
}
Meti

Slug dalam PHP

$slug = strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $string)));
CodePadding

Jawaban yang mirip dengan “Slug dalam PHP”

Pertanyaan yang mirip dengan “Slug dalam PHP”

Lebih banyak jawaban terkait untuk “Slug dalam PHP” di PHP

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya