How can I strip undisplayable characters from a string with PHP? [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a feed that pulls from social feeds. These feeds can sometimes include special characters such as emojis that don't translate well:
I just want to strip out any characters that can't be displayed. I know that might remove a few that I could want but that's fine. I know I can do a regex but not really sure how to target like what's in the photo.
---EDIT---
This is what is throwing the characters above:

Try using PHP Sanitize Filters, most specifically FILTER_SANITIZE_SPECIAL_CHARS
<?php
$a = 'text including "unprintable" characters';
$sanitized_a = filter_var($a, FILTER_SANITIZE_SPECIAL_CHARS);
echo $sanitized_a;
?>
Just be careful using this if there are some characters you want (like < and > for instance)! Check the link at the top for a full list of filters you can choose from.

Related

Get only all the visible text from a url [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I want to get all the visible text from a url. I need to clear all html code and get plain text.
The process does not have to be perfect, but I would like the text to be as clean as possible.
Do you know any way to make it relatively simple?
Thanks!
Javi.
Have a look at strip_tags function.
strip_tags — Strip HTML and PHP tags from a string
It may do the job.
You can get url the value of the query string using $_GET['the names in there']. and use strip_tags to get only the text.
function get_url(){ return $_GET['name userd'];}
and use something like this

how to create a fast translator using php like google translator? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I want to translate text in my website like google translate (Not with it). I just need a sample php code that translate some of defined words (not the whole language).
here is my example code:
$text = "hello john";
$translated_text = str_replace("hello", 'Hallo', $text);
echo $translated_text;
does it execute fast on the server?
do i have to translate word by word?
You need to fill a dictionary in order to lookup for the words you want to translate.
Take a look at this answer, the code is meant to filter bad words, but should work in your case.

PHP Place each word in a paragraph in a <span> without punctuation [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
How do I place each word in a paragraph in its individual span tag? Currently I am using explode() and using a space to do this, but the I get the following:
"<span>Hello,</span> <span>I</span> <span>am</span> <span>Alec.</span>"
I will be using a dictionary lookup for each word, but "Hello," will not find what I'm looking for. Furthermore, I am stylizing the span depending on the word selected (through an onClick), so I don't want the comma to be part of the span.
Eventually, I want the following code to populate:
"<span>Hello</span>, <span>I</span> <span>am</span> <span>Alec</span>."
Theoretically it would be nice to do this without using explode() since I have to add all of these characters back into the text, but outside of the <span>. Is there a function to accomplish this?
Thank you!
Edit: I will be using this for everyday writing in different languages, so hyphenated words or letters outside of "A-Za-z" are expected.
preg_replace() can do that:
$str = 'Hello, I am Alec.';
echo preg_replace('/(\w+)/', '<span>${1}</span>', $str);
Output:
<span>Hello</span>, <span>I</span> <span>am</span> <span>Alec</span>.

How to sanitize textarea input for specific tags using php [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have reworded this question please disregard original comments
UPDATED QUESTION
If I want to remove one or more characters in a textarea using php, how would I do this?
MY SOLUTION FOR A SINGLE INSTANCE
str_replace("http://","",$_POST['Website']);
How would I remove/replace multiple items or lower upper instances, i.e. HTTP//: http//: HTTPS:// https//: hTTps//: ?
You either should have fixed dictionary for allowed tags and good PCRE knowledge (remember to use lazy quantifiers) or (and this I would advise) use markdown syntax (there are js editors) and compile it in php to valid html.

Allow some html tags [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I need to allow certain html tags (br,p and strong) and I have encountered this code.
(strip_tags($this->parent->content[$i]['text']), $this->parent->config, 'portal_mode_grid_news_text_length', '…').'</p>';
All I need is to allow those tags I have stated previously, but I don't know how to do it.
If you look at the documentation for strip_tags, it shows that the second parameter is "allowed tags", so an example of how to allow br, p and strong is like so:
$string = "Hello<br>";
echo strip_tags($string, "<br><br/><p><strong>");
Adding br and br/ together allows both types of line break as opposed to just one.
(strip_tags($this->parent->content[$i]['text'], "<br><br/><p><strong>"), $this->parent->config, 'portal_mode_grid_news_text_length', '…').'</p>';

Categories