Link to home
Start Free TrialLog in
Avatar of Michael
Michael

asked on

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

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

SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Michael
Michael

ASKER

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
Avatar of Michael

ASKER

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

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.