Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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,018 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 59

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 59

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article was originally published on Monitis Blog, you can check it here . Today it’s fairly well known that high-performing websites and applications bring in more visitors, higher SEO, and ultimately more sales. By the same token, downtime…
When the s#!t hits the fan, you don’t have time to look up who’s on call, draft emails, call collaborators, or send text messages. An instant chat window is definitely the way to go, especially one like HipChat. HipChat is a true business app. An…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

636 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