Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Remove «  and    from text in php

Posted on 2016-08-05
4
Medium Priority
?
234 Views
Last Modified: 2016-08-05
Hi;

I want to remove html tags and also tags like: «  /    /»  from text and get clean text,i use the function strip_tags() but this function cannot remove all of these tags,need a function that clean completely a text in php.

Thanks in advance
0
Comment
Question by:MOSTAGHASSI
  • 3
4 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 41744804
You can use str_replace() for this.  I'll try to show you an example in a minute.
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 41744812
Try this.  You can add other things to the $unwanted array if you need to modify the replacement patterns.
https://iconoun.com/demo/temp_mosta.php
<?php // demo/temp_mosta.php
/**
 * https://www.experts-exchange.com/questions/28961805/Remove-laquo-and-nbsp-from-text-in-php.html
 *
 * http://php.net/manual/en/function.strip-tags.php
 * http://php.net/manual/en/function.str-replace.php
 */
error_reporting(E_ALL);


function tidy($str)
{
    $unwanted =
    [ '&laquo;'
    , '&nbsp;'
    , '&raquo;'
    ]
    ;

    $new = strip_tags($str);
    $new = str_replace($unwanted, '', $new);
    return $new;
}

$old = '<div>Foo&raquo;</div>';
$new = tidy($old);

echo htmlentities($old);
echo '<br>';
echo htmlentities($new);

Open in new window

0
 

Author Closing Comment

by:MOSTAGHASSI
ID: 41744830
Thanks Ray
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 41744992
:-)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

963 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