I need to convert the title of a node, for example, from this

John Doe



Is there Drupal function for it? (Easier to use than using preg_preplace etc.)

  • Hi, glad you got an answer but what you're asking has nothing to do with Drupal, and I think keeping this open is going to confuse people as it's entirely a PHP issue. I removed the confusing terminology about "human readable node names" etc, and changed the question only to contain what you're really asking – Clive Nov 28 '13 at 13:32
  • 2
    I don't fully agree with "off-topic". There is drupal_html_class($class) function in core. – milkovsky Sep 3 '14 at 10:52
  • 2
    I don't think this question is off-topic either. module_load_include('inc', 'pathauto', 'pathauto'); $hyphened = pathauto_cleanstring('John Doe'); – Francisco Luz Nov 18 '14 at 7:30
  • 1
    not off-topic, Drupal has functions to achieve a lot of things that PHP sucks at. – scor Sep 11 '15 at 16:31

If you're just looking for a way to convert the title into a URL friendly machine name, then the following regex will do what you need:

$machine_name = preg_replace('@[^a-z0-9-]+@','-', strtolower($title));
  • Thank you @littledynamo This is exactly what I needed. Also thank you for making my question clearer. – Boutros AbiChedid Nov 28 '13 at 13:16
  • 1
    @BoutrosAbiChedid Bear in mind this method will not mimic your pathauto settings - if you change those, to exclude/include a certain character, for example, this method will not keep up-to-date. It's much better to use the API methods (if you're not using Pathauto just ignore this) – Clive Nov 28 '13 at 13:20
  • @littledynamo I am not using Pathauto so your method is great. Thanks. – Boutros AbiChedid Nov 28 '13 at 13:21
  • You can also user $name = str_replace(' ', '-', strtolower('John Doe')); – inizio Nov 28 '13 at 14:48
  • Yeah that will work and is more performant (provided the title doesn't contain any slashes or such like). – littledynamo Nov 28 '13 at 16:16

Check this answer.

If you are on the node template (ie. node.tpl.php), you should have $node->nid available.

  • Not sure how this answers the question? $node->nid returns a number, not at all what the OP asked. – user1731 Jun 13 '17 at 12:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.