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.
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 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.
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().
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.
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 ' 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.
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
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.