“Slug in Php” Code-Antworten

Saite zu 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 Get Slug

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

Slug mit PHP erstellen

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 in Php

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

Ähnliche Antworten wie “Slug in Php”

Fragen ähnlich wie “Slug in Php”

Weitere verwandte Antworten zu “Slug in Php” auf PHP

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen