Solved

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

Posted on 2014-04-12
5
999 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 57

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 35

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 57

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

Get Actionable Data from Your Monitoring Solution

Your communication platform is only as good as the relevance of the information you send. Ensure your alerts get to the right people every time with actionable responses. Create escalation rules that ensure everyone follows the process and nothing is left to chance.

Question has a verified solution.

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

Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
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…
This video teaches users how to migrate an existing Wordpress website to a new domain.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

695 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