sergekol
sergekol

Reputation: 123

JSON-LD and quotes in the content

I have an issue with validating JSON-LD code by Google Structured Data Testing Tool. My article text is taken from MySQL database and put into JSON-LD structure by php script along the following lines:

<script type="application/ld+json">
{
    "@context": "http://schema.org",
    "@type": "Article",
    ...
    "articleBody": "<?php echo article content here ?>",
    ...
}

The problem is that if the article text contains quotation marks (") then they conflict with JSON-LD block syntax and cause validation error. Basically the very first instance of (") in the article content indicates the end of articleBody and the next (") causes syntax error.

The only idea I have is to pre-process article content with php and remove (") symbols. This works fine, but looks artificial:

    "articleBody": "<?php echo str_replace( '"', '', article content here) ?>",

Are there any standard workarounds available?

Upvotes: 4

Views: 4471

Answers (3)

CQD
CQD

Reputation: 31

Since you are trying to output JSON, I would suggest use json_encode instead, which should be more straightforward, and less likely to be broken.

<script type="application/ld+json"><?=json_encode([
    "@context" => "http://schema.org",
    "@type" => "Article",
    "articleBody" => $articleBody,
    ...
])?></script>

Result will be:

<script type="application/ld+json">{"@context":"http:\/\/schema.org","@type":"Article","articleBody":"Your article body"}</script>

Upvotes: 2

Jakyo
Jakyo

Reputation: 52

As essexboyracer already said, you're looking for the function addslashes()

https://www.php.net/manual/en/function.addslashes.php

Returns a string with backslashes added before characters that need to be escaped. These characters are:

single quote (') double quote (") backslash () NUL (the NUL byte)

Upvotes: 1

sergekol
sergekol

Reputation: 123

Thank you Unor. I ended up with the following script which works just fine:

"articleBody": "<?php echo preg_replace( '/\s+/', ' ', str_replace( '"', '\"', wp_strip_all_tags( get_the_content() ) ) ) ?>",

Upvotes: 1

Related Questions