John Davidson

How to remove in PHP all HTML <sup> and some CSS Classes from a PHP variable?

0 comments
Message:


I have a PHP variable named $matches[0] which contains a HTML Code.


So, I want to know if is there a way to remove in PHP any from $matches[0] variable these HTML Tags and/or CSS class:



  • <sup>...</sup> (HTML Tags) ???

  • class="navigation-only" (CSS Class) ???

  • class="noprint wikidata-linkback" (CSS Class) ???


If so, how to do to remove all HTML <sup> and navigation-only and noprint wikidata-linkback CSS Classes ???


Because it will very help me.



My Answer:
You can use the `strip_tags()` function in PHP to remove HTML tags from a string. You can also use `preg_replace()` to remove specific CSS classes from the string.

Here's an example of how you can remove `` tags and the specified CSS classes from your `$matches[0]` variable:

php
// Remove tags
$matches[0] = strip_tags($matches[0], '');

// Remove class="navigation-only"
$matches[0] = preg_replace('/class="navigation-only"/', '', $matches[0]);

// Remove class="noprint wikidata-linkback"
$matches[0] = preg_replace('/class="noprint wikidata-linkback"/', '', $matches[0]);

// Output the modified string
echo $matches[0];


This code will remove all `` tags and the specified CSS classes from the `$matches[0]` variable. You can adjust the regular expressions in the `preg_replace()` function to match other CSS classes as needed.

Rate this post

5 of 5 based on 1376 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us