?
Solved

jQuery add to add CMS record to <meta name="description" content=""> and remove all html.

Posted on 2014-04-12
5
Medium Priority
?
1,011 Views
Last Modified: 2014-04-12
I need to add content to the <meta name="description" content=""> using a field from a CMS field <?php echo $storyData['comment']; ?>. The code below achieves this and works fine, but the problem is that it also includes html that I need to remove such as:

<h1></h1>
<p></p>
&nbsp;

So this is what it looks like: <meta name="description" content="<h1>Wow!</h1><p>The dogs jumped the fence &nbsp; to welcome our guest</p>">

Below is the code I'm using to add the CMS field <?php echo $storyData['comment']; ?> to the meta description - content, but I can't figure out how to strip out the:

<h1></h1>
<p></p>
&nbsp;

Anyone have suggestions on how I can remove the html markup (or even better a solution to remove all html markup) leaving only plain text to place into the  <meta name="description" content="Wow! The dogs jumped the fence to welcome our guest"> so it read as seen in this sentence?



The CMS field <?php echo $storyData['comment']; ?> returns the results = <h1>Wow!</h1><p>The dogs jumped the fence &nbsp; to welcome our guest</p>

<meta name="description" content="">

<script type="text/javascript">
jQuery(document).ready(function($) { 

	$('meta[name=description]').attr('content', '<?php echo $storyData['comment']; ?>');

})
</script>

Open in new window

0
Comment
Question by:Michael
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 58

Assisted Solution

by:Julian Hansen
Julian Hansen earned 800 total points
ID: 39995913
Firstly look at the php strip_tags function
http://www.php.net/strip_tags

Secondly - why are you doing this in javascript - why not write the meta information directly at page creation?
0
 
LVL 35

Accepted Solution

by:
Dan Craciun earned 1200 total points
ID: 39995914
Try, instead of <?php echo $storyData['comment']; ?>
<?php echo strip_tags($storyData['comment']); ?>

HTH,
Dan
0
 
LVL 2

Author Comment

by:Michael
ID: 39996137
Secondly - why are you doing this in javascript - why not write the meta information directly at page creation?

I usually do create the meta description at the time of the page creation, but in this particular case - data is being imported in the CMS via a CSV file which includes up to 200+ records on a weekly basis by the client and that's the catch. I have to build something that will generate the meta description off of a particular record field entry that will allow the client the ability to import the 200+ records and get back to business - with no need to manage the meta descriptions.

I'll have a look at strip_tags
0
 
LVL 2

Author Comment

by:Michael
ID: 39996284
Thanks for the help... I added preg_replace to remove a few other remaining elements.

<meta name="description" content="">

<script type="text/javascript">
jQuery(document).ready(function($) { 

	$('meta[name=description]').attr('content', '<?php echo preg_replace("/&#?[a-z0-9]{2,8};/i", " ", strip_tags($storyData['comment'])); ?>');

})
</script>

Open in new window

0
 
LVL 58

Expert Comment

by:Julian Hansen
ID: 39996285
Yes ok but you can still build the meta tag dynamically based on the data - you are doing it already in javascript by outputing the value to a javascript call - just not sure why you can't put that into a page variable that you use in your template to output the meta value.

I do this all the time - all meta / title data is page specific and generated with the page - the template calls a function that gets the relevant meta tag for the page being generated at render time.

You can do it your way but seems like a bit of a complicated way of doing it. If you do have to do it that way then strip_tags is what you are looking for.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question