Jump to content

How to strip quotes (single and double) from a string in PHP


Recommended Posts

I have used a class called clsMetaContent.php, as provided in the book Creating Interactive Websites with PHP and Web Services, by  Eric Rosebrock - who I understand is the lead developer of this site.

 

While using this class with my application - I find a problem in the assignment of the description for the description part of the meta tag.

 

The code in the class looks like this:

 

// Formulate the description for each page.
      if(empty($ptitle)){
         $description = $this->description;
      } else {
         $description = '$ptitle - $this->description";

 

I have found that my description can contain single and/or double quotes. Which causes a problem with the assignment of the meta tag for the description. What happens is, due to the error in the format of the meta tad description, it outputs the contents of description again on the start of my web page, instead of keeping it within the meta tag assignment.

 

I think I just need a function to strip out double and single quotes - but have been unable to find any.

Can anyone offer some advice?

 

Thanks in advance.

 

I have been trying this without success:

 

// Formulate the description for each page.
     if(empty($ptitle)){
        $description = $this->description;
     } else {
        $description = "$ptitle - $this->description";
     }
  $description = str_replace('"', '\"', $description); 
  $description = str_replace("'", "\'", $description);

 

Trying to replace double quote in first str_replace and replace single quote in second. ie. replace with \ so that its escaped, instead of removing it.

Sorry, was posting that while you posted your suggestion sasa. I have implemented your suggestion now,and it removes slashes, but I still have an error with meta description. Highlights in red colour (as error) in Firefox. This is what is being assigned:

 

<META NAME="DESCRIPTION" CONTENT="Hillside is offered as a project that encompasses the plan for a 15-unit apartment complex located within 3km from the Albena beach resort and the development site (land plot).<br /><br />The apartments would comprise of studio, 1 and 2 bedrooms, with views over neighbouring hillside. The plan includes 4 levels, one of which is designated to provide recreational facilities such as a gym, cinema room,  games room.<br /><br />The concept also incorporates an outdoor swimming pool with bar area.<br /><br />Located in a village that can be reached within 25 minutes from the Varna International Airport, this makes it a very attractive proposition for investors as a key factor for the selection of a rental apartment.<br /><br />With just a 5 minute drive to the very popular Albena beach resort, it is ideally situated for tourists, and can provide additional accommodation for the tourist market.<br /><br />What is even more attractive is that there are two Gary Player designed golf courses under construction – which are within 15 and 30 minutes drive of this "Hillside" development – providing even more recreational facilities.  These are known as "BlackSeaRama" and "Thracian Cliffs" and denote signature Gary Player courses.<br /><br />•	Architectural plans for the "Hillside" apartment complex<br />•	3D model of the development (3DStudio Max)<br />•	Regulated land plot for the development site<br />•	Land plot documentation (title deed and building visa – "PUP")<br />•	Original brochure template for brochure print runs (advertising material)<br />•	3D photographs of the "Hillside" development<br />•	Photographs of the village (for advertising purposes)<br />•	"Hillside" logo<br />•	"Hillside" advertising material / information pack / marketing style<br />•	Colour floor plans for each level<br />•	Web site material for advertising the development (HTML files and related images)<br /><br />Please note that individual apartment units are NOT for sale.<br />">

 

Not sure if its the bullet points thats causing an issue with it. It highlights the content up to Gary player courses in red.

This seems to have fixed it:

 

// Formulate the description for each page.
  
  $this->description = strip_tags($this->description);
      if(empty($ptitle)){
        $description = addslashes($this->description);
      } else {
       $description = addslashes($ptitle) . ' - ' . addslashes($this->description);
 }

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.