Solved

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

Posted on 2014-04-12
5
985 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
  • 2
  • 2
5 Comments
 
LVL 51

Assisted Solution

by:Julian Hansen
Julian Hansen earned 200 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 34

Accepted Solution

by:
Dan Craciun earned 300 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 51

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now