Dealing with apostrophes

Spaces are a problem because it is not easy to distinguish how many exist if more than one space is used.

As mentioned the source URL is a link and will be ignored for SEO purposes because the pages are private requiring password access.

Using a table as a cross reference to link the displayed title and relevant source URL makes it easy to add, delete, modify songs.

Hyphens and apostrophes add complexity which is not required for the link.

Itā€™s not a problem for a filename on your local file system, but it is a problem for URLs on the internet.
You can encode URLs as mentioned, but itā€™s best not to have to.

@SamA74,

I added spaces so Sitepoint didnā€™t interpret it as an actual commandā€¦

Same if I used @apos; ?

Right, I had to use single quotes with my PHP and that is where I ran into trouble.

So in summary, simply use the hex code for special characters that can reak things like a single quote or apostrophe and my PHP/HTML will be okay?

On a related note, why wonā€™t the < audio > element read files with apostrophes/single quotes in them? (e.g. ā€œJourney - Whoā€™s Crying Now.mp3ā€)

Same issue?

And how would I address that? Because it is pretty natural to have file names with special characters in them in the modern world since both macOS and Windows accept most characters and it makes things easier to read - and more accurate from an English standpoint!!

So if I had created a database that stored the songā€™s ā€œDisplay Titleā€ and ā€œFilenameā€ and so on, then I could reduce issues?

Of course, there would still be the problem that on my laptop, I want the file names to be as close to proper English as possible for readability. And if I am referencing those same songs for a website, then that creates a conflict.

Fortunately for this photo site, there are just a few dozen songs.

So I guess your advice would be to edit out spaces and apostrophes in the files that I serve up - in other words create a second set of music files for my webserver, and that way there arenā€™t issues with my website but I can keep the original files locally on my laptop with the preferred formatting, right?

Use &apos; or &#39; in HTML, either will do.

Use hex in URLs, use the name or number in HTML.
PHP is different again, depends on context.
In a string you can escape with a backslash:-

$string = 'Journey - Who\'s Crying Now.mp3' ;

Though any sensible person would just say:-

$string = "Journey - Who's Crying Now.mp3" ;

But there may be cases where you need both types of quote in a string.

Note on outputā€¦

echo $string ;

ā€¦would giveā€¦

Journey - Who's Crying Now.mp3

ā€¦so you may want toā€¦

echo htmlentities($string);

Though you should be doing that, or at least echo htmlspecialchars($string); for security purposes anyway.

As stated:-

Encode special characters in URLs, either with urlencode() in PHP or by manually substituting characters for % followed by the hex code (you can just use + for a space).
Or better still, donā€™t use those characters for on-line files and URLs.

1 Like

@SamA74,

I think part of my problem is that my website is more HTML than PHP and so I cannot easily take advantage of the PHP functions unless I am willing to rewrite most of my HTML with PHP echos.

I also confess that I have never really understood or used functions like urlencode() or htmlspecialchars().

Maybe that can be my 2020 resolution? :wink:

Thanks!

A database is not required just a list similar to the following:

$aLinks = [
    'audio-001' => "Journey - Who's Crying Now",
    'audio-002' => "Zombies - Time Of The Season",
    'audio-003' =>  "Queen - Don't Stop Me Now", 
    'audio-004' =>  "Moody Blues - It's Up To You",
    'audio-005' =>  "Elvis Presley - Can't Help Falling In Love",
];

None of Googleā€™s links are readable (and may and have a copyright).
The following PHP could make the link readable:

//=====================================
// Usage: 
//   $url = makeUrl( $title); 
//=====================================
function makeUrl( string $title) : string
{
$old = [
' ',
"'",
];
$new = [
'-',
'',
];

$result = str_replace($old, $new, $title);
$result = strtolower($result); // reduces case errors

return $result;
}//

$title = "Journey - Who's Crying Now",;
echo $link = makeUrl($title);
// output  "journey---whos-crying-now";
1 Like

That is completely wrong.
Just change the extension to php.
But you will need to setup a local server on your mac.
If you havenā€™t set up your local server this site and are running Catalina

is the goto site. If not on Catalina search the site for your OS x version.

I run MAMP currentlyā€¦

So if apache2 is running change the extension to php and insert php code between php tags

<?php // php code ?>

And Bobā€™s your uncle.

1 Like

No, Bob isnā€™t my uncle, and you missed the pointā€¦

Earlier I said that I preferred to avoid taking an entire page written in HTML and converting it to ugly PHP echo statements for the sake of using a PHP function.

For a larger site, that might make sense, but here I was hoping for an easier solution.

And Iā€™ve sorta lost my way in this thread, but I believe the advice has been to add HTML numbers &#39; or HTML names @apos; to my HTML code and avoid apostrophes in file names.

My point is that if your page is being served by Apache2 or another web server capable of serving php files, that html code will be served exactly the same as if it was a html file.
Apache2 only invokes the php interpreter when it encounters php tags.
The ā€œ<?phpā€ tag causes Apache2 to send everything between the php tags to the php interpreter, and then resume serving html.
You do not have to convert your entire page to echo statements.

1 Like

So if I had a web page with thisā€¦

<p>Bob's favorite song is "Journey's - Who's Cryin' Now",</p>

<p><a href="/music/Journey - Who's Crying Now.mp3>Listen here</a></p>

How are you proposing to rewrite the code using a mixture of HTML and PHP?

<html>
    <head>
    </head>
    <body>
        <div>
            <h2>Test php htmlentities</h2>
            <p><?php
                echo htmlentities("Bob's favorite song is ''Journey's - Who's Cryin' Now''<>");
            ?></p>
        </div>
    </body>
</html>

Note You canā€™t have both single and double quotes within the same string unless you break it up and rejoin it so I have simulated the double quotes with two single quotes.
However, from memory, you can have both in heredoc strings and v7.? has made heredoc a lot more attractive and easier to use.

It all comes down to how you are creating your html

1 Like

@dennisjn,

Whatā€™s up with the <> at the end?

You can is you escape them.

<?= htmlentities("Bob's favorite song is \"Journey's - Who's Cryin' Now\"", ENT_QUOTES);?>

or

<?= htmlentities('Bob\'s favorite song is "Journey\'s - Who\'s Cryin\' Now"', ENT_QUOTES);?>

Note you need to add the ENT_QUOTES flag to encode the single quotes too.

1 Like

Yeh the <> was missed character selection when moving the p tags out of the string.
However an even better solution which enables having both single and double quotes is using the heredoc operand to define the string.

<html>
    <head>
    </head>
    <body>
        <div>
            <h2>Test php htmlentities</h2>
<?php
echo <<<EOT
<p>Bob's favorite song is "Journey's - Who's Cryin' Now"</p>
EOT;
?>
        </div>
    </body>
</html>

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.