Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6669977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:12:51+00:00 2026-05-26T03:12:51+00:00

For a YouTube web-app I’m building in PHP, I have the simple video player,

  • 0

For a YouTube web-app I’m building in PHP, I have the simple video player, embedded with an <iframe>, and then a <div> with information about the currently loaded video (description, id, title, etc).

A <ul> contains a list of videos which are fetched using the PHP gData API from YouTube, and each <li> contains a link which activates JavaScript to change the video player to the correct video and also update the video info on the page.

Here’s the issue: gData returns a multi-line, non-escaped sequence for the video description, which doesn’t work in JavaScript. How should I remove line breaks and replace them with <br> (note that they aren’t line breaks like \n, they are actual line breaks and newlines).

I also have to escape other things that won’t work in a JavaScript string, such as the apostrophe character '. What is the best way to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:12:52+00:00Added an answer on May 26, 2026 at 3:12 am

    Marc B has given the best answer. Use json_encode: http://php.net/manual/en/function.json-encode.php Go, upvote his answer.

    The following is my original response:

    <?php
    $data = "Hello, 'world'.\nHow are you doing?\n\"Good?\"\n";
    $data = str_replace("\n", '<br>', $data);
    $data = str_replace('"', '\"', $data);
    $data = str_replace("'", "\'", $data);
    echo $data;
    ?>
    

    The same stuff using regex:

    <?php
    $data = "Hello, 'world'.\nHow are you doing?\n\"Good?\"\n";
    $data = preg_replace("/\n/", '<br>', $data);
    $data = preg_replace("/\"|'/", '\"', $data);
    echo $data;
    ?>
    

    Having given those examples, you don’t really need to escape both single-quotes and double-quotes. In JavaScript, you can use double-quoted strings as well as single-quoted strings. So, use one and escape the other.

    You might also want to escape backslash (replace \ with \\) to make sure that some funny YouTube uploader doesn’t try to break your PHP script by placing a foo\'bar in the video description. Now, that can break your script if you don’t escape backslash because the JavaScript string after replacements would now look like: 'foo\\'bar' which is a syntax error because the string finishes at 'foo\\'.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a YouTube video inside of a <div> on my web page. When
I have a YouTube's player in the webpage. I need to change the video
Is it possible to have a YouTube video played on a web page in
I am building a web site in PHP that integrates with Youtube. I want
I have a view with a web view which loads a YouTube video. I
Im embedding a youtube video in JWPlayer like this: <embed src=fileadmin/templates/web/js/jwplayer/player.swf width=300 height=225 bgcolor=transparent
I am working with a CMS for a web app in PHP, that has
I finally got my jQTouch/iPod web app embedding a video . Thanks to Ryan
I have a requirement to play YouTube video content from within an Android application
I am building a web app where I need to get all the images

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.