Skip to main content
added 18 characters in body
Source Link
cjbj
  • 15k
  • 16
  • 42
  • 89

Indeed, the the_title function echoes the title, unless you set the third parameter to false (default true). So you start with this;

$title = the_title ('','',false);

Now, $title may contain all kinds of characters that cannot be used in PHP variable names, so we need some sanitization:

$title = preg_replace('/[^A-Za-z0-9\_]/', '', $title); // Removes special chars except underscore.

Then you use PHP variable variables to turn the content of $title into a variable name:

$$title = 'something';

So, if the_title gives the value "hello world", the $title variable after sanitizing has the value "helloworld" and $helloworld has the value "something".

Indeed, the the_title function echoes the title, unless you set the third parameter to false (default true). So you start with this;

$title = the_title ('','',false);

Now, $title may contain all kinds of characters that cannot be used in PHP variable names, so we need some sanitization:

$title = preg_replace('/[^A-Za-z0-9\_]/', '', $title); // Removes special chars.

Then you use PHP variable variables to turn the content of $title into a variable name:

$$title = 'something';

So, if the_title gives the value "hello world", the $title variable after sanitizing has the value "helloworld" and $helloworld has the value "something".

Indeed, the the_title function echoes the title, unless you set the third parameter to false (default true). So you start with this;

$title = the_title ('','',false);

Now, $title may contain all kinds of characters that cannot be used in PHP variable names, so we need some sanitization:

$title = preg_replace('/[^A-Za-z0-9\_]/', '', $title); // Removes special chars except underscore.

Then you use PHP variable variables to turn the content of $title into a variable name:

$$title = 'something';

So, if the_title gives the value "hello world", the $title variable after sanitizing has the value "helloworld" and $helloworld has the value "something".

Source Link
cjbj
  • 15k
  • 16
  • 42
  • 89

Indeed, the the_title function echoes the title, unless you set the third parameter to false (default true). So you start with this;

$title = the_title ('','',false);

Now, $title may contain all kinds of characters that cannot be used in PHP variable names, so we need some sanitization:

$title = preg_replace('/[^A-Za-z0-9\_]/', '', $title); // Removes special chars.

Then you use PHP variable variables to turn the content of $title into a variable name:

$$title = 'something';

So, if the_title gives the value "hello world", the $title variable after sanitizing has the value "helloworld" and $helloworld has the value "something".