Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Possibile Inflector::friendly_title bug ?
  • Hi guys,
    today I've tried to use friendly_title function but, I think, there's a possibile bug. Example :
    // Stress
    $test = "Mess'd up --text-- just (to) stress /test/ ?our! `little` \\clean\\ url fun.ction!?-->";
    // return : mess'd-up-text-just-(to)-stress-/test/-our!-`little`-\clean\-url-fun.ction!->
    // #WRONG
    
    // Italian
    $test = "ti diamo il benvenuto all'Hotel&Restaurant; dove la cortesia è di casa";
    // return : ti-diamo-il-benvenuto-all'hotel&restaurant;-dove-la-cortesia-e-di-casa
    // #WRONG
    
    // French
    $test = "Peux-tu m'aider s'il te plaît?";
    // return : peux-tu-m'aider-s'il-te-plait
    // #WRONG
    
    // Swedish
    $test = "Tänk efter nu – förr'n vi föser dig bort";
    // return : tank-efter-nu--frr'n-vi-fser-dig-bort
    // #WRONG
    
    echo \Inflector::friendly_title($test, $sep = '-', $lowercase = true);
    
    in friendly title there are bad url characters ...
    This function of Inflector is right to create a page title slug or I'm wrong? Cheers
  • You're right, the friendly_title() method has gotten way too little attention. Not on the top of any of our priority lists though, so if you want it fixed I'd suggest editing it and pull-requesting.
  • I've changed Inflector::friendly_title() function ( not commited yet ) : http://scrp.at/4b Someone have any suggestion or can help me to add other tests? Cheers
  • Hey David, I had written a similar piece of code to format directory names for storing uploaded files. I didn't realize friendly_title() existed. I'm going to start using this method along with your updated code. I'll let you know if I run into any problems. One question...in the regular expression you used... /[^a-zA-Z0-9\/_|+ -]/ I know that ^a-zA-Z0-9 matches lowercase & uppercase a-z, and 09, and that \ escapes / so you can match / and _. I read that | means match on either side. What does "|+ -" accomplish? The function I was using was much simpler: preg_replace('#[^a-z0-9]#', $sep, strtolower($name))
  • Calvin Froedge wrote on Friday 17th of June 2011:
    Hey David, I had written a similar piece of code to format directory names for storing uploaded files. I didn't realize friendly_title() existed. I'm going to start using this method along with your updated code. I'll let you know if I run into any problems. One question...in the regular expression you used... /[^a-zA-Z0-9\/_|+ -]/ I know that ^a-zA-Z0-9 matches lowercase & uppercase a-z, and 09, and that \ escapes / so you can match / and _. I read that | means match on either side. What does "|+ -" accomplish? The function I was using was much simpler: preg_replace('#[^a-z0-9]#', $sep, strtolower($name))
    I have problems with $lowercase parameter using your code :
    preg_replace('#[^a-z0-9]#', $sep, strtolower($name))
    

    If you test :
    $test = "<b>Fuel</b> is a community driven PHP 5 web framework";
    echo \Inflector::friendly_title($test, $sep = '-', $lowercase = false) . '<br/>';
    

    The result is :
    fuel-is-a-community-driven-php-5-web-framework <-- WRONG instead of :
    Fuel-is-a-community-driven-PHP-5-web-framework <-- RIGHT
  • I didn't know friendly_title function existed when I wrote that piece of code. But to solve bug you mentioned... if($lowercase)
    return preg_replace('#[^a-z0-9]#', $sep, $name); return preg_replace('#[^a-z0-9]#', $sep, strtolower($name)); Still much simpler...
  • Ok calvinfroedge,
    I've optimized the RegEx ... Your code is wrong
    if($lowercase)
    return preg_replace('#[^a-z0-9]#', $sep, $name); return preg_replace('#[^a-z0-9]#', $sep, strtolower($name));

    You cannot make this : reg_replace('#[^a-z0-9]#', $sep, $name);
    Or parts of urls ( in Uppercase ) will cutted. But is a very simple fix :
    $str = preg_replace("#[^a-z0-9]#i", $sep, $str);
    

    Add "i" after "#" to make an insensitive regex search. Now inflector::friendly_title() is ok
    Thank you for your help!

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion