Jump to content
php.lv forumi

PHP mainīgā pārveidošana


ArnisR

Recommended Posts

$sometext = "Šeit ir kaut kāds teksts";

 

Kā PHP mainīgo sometext pārvērst par šādu:

 

$sometext = "seit-ir-kaut-kads-teksts";

 

Tātad, kā redzams, visas atstarpes aizvietojās ar "-", un latviešu burtiem tika noņemtas mīkstinājuma un garumzīmes, kā arī lieli burti pārtapa par maziem.

Link to comment
Share on other sites

Internetā to meklē kā "slug".

Es izmantoju šādu variantu.

  function toSlug($s){
   if (function_exists('iconv')) {
     $s = @iconv('utf-8', 'us-ascii//TRANSLIT', $s);
   }
   $s = preg_replace("/[&<>]/", " ", $s);
   $s = preg_replace("/[^a-zA-Z0-9 -]/", "", $s);
   $s = preg_replace('/[\s]+/', '-', $s);
   $s = mb_strtolower($s);
   $s = trim($s, " \t. -");
   return $s;
 }

Link to comment
Share on other sites

Noderīgo funkciju topika šī lieta tika pieminēta vairākas reizes, rekur 2 izceltās:

http://php.lv/f/topic/15423-noderigas-funkcijas/page__p__118661#entry118661 - izskatās bišķ šačakarēta

http://php.lv/f/topic/15423-noderigas-funkcijas/page__p__118666#entry118666 - ereg sucks

 

P.S. +1 codez

Edited by marcis
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...