Removing new lines and escape characters introduced when reading file in PHP - php

{
"elements": [
{
"cardtype": "textcard",
"title": "Final Fantasy",
"titlesize": "medium"
}
]
}
The above is content of a file. I want to return this in response. I use file_get_contents to read the contents, however, I get this:
{\n \"elements\": [\n {\n \"cardtype\": \"textcard\",\n \"title\": \"Final Fantasy\",\n \"titlesize\": \"medium\",\n ...
The new lines and escaping is not what I want. Is there some way to avoid that?

use following code for removing \n from you data.
$fileData = str_replace(array("\r", "\n"), '', file_get_contents($fileName));

To get rid of escape characters use stripslashes().
To get rid of \n use str_replace().
<?php
$string = file_get_contents("file.txt");
$string = str_replace("\n", "", $string);
$string = stripslashes($string);
?>
UPDATE:
Try this.
$string = file_get_contents("file.txt");
$string = str_replace("\n", "", $string);
$string = str_replace("\\", "", $string);

Related

Problems with reading objects from another file with php

I have some file called main.config which has:
{"name":"wtf","image":"main.PNG","desc":"This is about this that and that.","tags":{"php","js","html"}}
Now in php I am reading this file and getting its content like this:
$string = "";
$handle = fopen("main.config", "r");
while(!feof($handle)){
$string .= fgets($handle);
}
fclose($handle);
And now I have been trying to decode this but it always return either null or string with additional "" around.
"{"name":"wtf","image":"main.PNG","desc":"This is about this that and that.","tags":{"php","js","html"}}"
This is everything I tried:
json_decode( preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $string), true );
json_decode(preg_replace('/\s+/', '',$string), true);
json_decode(preg_replace('/\x{FEFF}/u', '', $string), true);
json_decode(str_replace('"', '"', $string));
utf8_encode($string);
stripslashes($string);
trim($string);
html_entity_decode($string);
json_decode(print_r($string, true), true);
But nothing seems to work, is it because I used some different file type or what could it be?
It was just an invalid json formatted string.
Change to:
{"name":"wtf","image":"main.PNG","desc":"This is about this that and that.","tags":["php","js","html"]}
You can also read the content with less code using:
json_decode(file_get_contents('main.config', true);
If you want to validate json format you can use this site also: jsonlint.com

Extracting words from a text using php

Hello friends have a little problem. I need to extract only the words of a text "anyone".
I tried to retrieve the words using strtok (), strstr (). some regular expressions, but only managed to extract some words.
The problem is complex due to the number of characters and symbols that can accompany the words.
The example text which must be extracted words. This is a sample text:
Main article: our 46,000 required, !but (1947-2011) mail#server.com March 8, 2014 Gutenberg's 34-DE 'a' 3,1415 Us: #unknown n go http://google.com or www.google.com and http://www.google.com (r) The 509th "composite" and; C-54 #dog v4.0 ¿as is done? ¿article... agriculture? x ¿cat? now! Hi!! (87 meters).
Sample text, for testing.
The result of extracting the text should be:
Main article our required but March Gutenberg's a go or and The composite and dog as is done article agriculture cat now Hi meters
Sample text for testing
The first function I wrote to facilitate the work
function PreText($text){
$text = str_replace("\n", ".", $text);
$text = str_replace("\r", ".", $text);
$text = str_replace("'", "", $text);
$text = str_replace("?", "", $text);
$text = str_replace("¿", "", $text);
$text = str_replace("(", "", $text);
$text = str_replace(")", "", $text);
$text = str_replace('"', "", $text);
$text = str_replace(';', "", $text);
$text = str_replace('!', "", $text);
$text = str_replace('<', "", $text);
$text = str_replace('>', "", $text);
$text = str_replace('#', "", $text);
$text = str_replace(",", "", $text);
$text = str_replace(".c", "", $text);
$text = str_replace(".C", "", $text);
return $text;
}
Split function:
function SplitWords($text){
$words = explode(" ", $text);
$ContWords = count($words);
for ($i = 0; $i < $ContWords; $i++){
if (ctype_alpha($words[$i])) {
$NewText .= $words[$i].", ";
}
}
return $NewText;
}
The program:
<?
include_once ('functions.php');
$text = "Main article: our 46,000 ...";
$text = PreText($text);
$text = SplitWords($text);
echo $text;
?>
Is that the code has a long way. We appreciate your help.
If I understand you correctly, you want to remove all non-letters from the string. I would use preg_replace
$text = "Main article: our 46,000...";
$text = preg_replace("/[^a-zA-Z' ]/","",$text);
This should remove everything that is not a letter, apostrophe or a space.
Try this almost your requirement
<?php
$text = <<<HEREDOC
Main article: our 46,000 required, !but (1947-2011) mail#server.com March 8, 2014 Gutenberg's 34-DE 'a' 3,1415 Us: #unknown n go http://google.com or www.google.com and
http://www.google.com (r) The 509th composite" and; C-54 #dog v4.0 ¿as is done? ¿article... agriculture? x ¿cat? now! Hi!! (87 meters). Sample text, for testing.
HEREDOC;
//replace all kind of URLs and emails from text
$url_email = "((https?|ftp)\:\/\/)?"; // SCHEME
$url_email .= "([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?#)?"; // User and Pass
$url_email .= "([a-z0-9-.]*)\.([a-z]{2,4})"; // Host or IP
$url_email .= "(\:[0-9]{2,5})?"; // Port
$url_email .= "(\/([a-z0-9+\$_-]\.?)+)*\/?"; // Path
$url_email .= "(\?[a-z+&\$_.-][a-z0-9;:#&%=+\/\$_.-]*)?"; // GET Query
$url_email .= "(#[a-z_.-][a-z0-9+\$_.-]*)?"; // Anchor
$text = preg_replace("/$url_email/","",$text);
//replace anything like Us: #unknown
$text = preg_replace("/Us:.?#\\w+/","",$text);
//replace all Non-Alpha characters
$text = preg_replace("/[^a-zA-Z' ]/","",$text);
echo $text;
?>

PHP remove newline in string for both Linux and Windows

Currently I have to call
$html = str_replace($search="\r\n", $replace='', $subject=$html);
$html = str_replace($search="\n", $replace='', $subject=$html);
to remove new line character in string $html. Is there a better/shorter way?
Try:
$html = str_replace(array("\r", "\n"), '', $html);
Yes, you can do that at once by using an array:
$search = array("\r\n", "\n");
$result = str_replace($search, $replace='', $subject=$html);
See str_replaceDocs.

Convert JSON in print format to valid JSON

I have a text file that is formatted like JSON, but in a print/view friendly format and I want to convert that string to valid JSON.
Basically, I want to read the file using PHP5 and call json_decode to deserialize the string.
But, json_decode is not able to parse the "print-friendly" json string.
I am getting error 4 Invalid or malformed JSON.
It looks like someone else had a similar issue as me: PHP json_decode() returns NULL with valid JSON?
I am using notepad++ to write the json file.
So, how can I convert
FROM:
{
"data": [
{
"thumbImg": "thumbImg",
"street": "street",
"city": "Fort Worth",
"state": "Texas",
"zip": "76192-0001",
"url": "url"
}
]
}
TO:
{"data":[{"thumbImg": "thumbImg", "street": "street", "city": "Fort Worth", "state": "Texas", "zip": "76192-0001", "url": "url"}]
I even tried doing the following:
<?php
$filename = "links.json";
$file = fopen($filename, "r");
$lines = file($filename);
$data = "";
;
foreach ($lines as $line_num => $line) {
$formatted = trim($line);
$formatted = str_replace("\r", "", $formatted);
$formatted = str_replace("\n", "", $formatted);
$data .= $formatted;
}
$json = json_decode($data, true);
?>
I did a var_dump of the resulting json string and http://jsonlint.com/ marked it as valid json; however, json_decode is not able to deserialize the json string for some reason.
Thank you!
SOLUTION
I set the encoding of the text file to UTF-8 without BOM and it works fine now. thank you all!
<?php
$filename = "links.json";
$file = file_get_contents($filename);
$json = json_decode($file, true);
?>
References:
- file_get_contents()
- json_decode()

Best way to convert title into url compatible mode in PHP?

http://domain.name/1-As Low As 10% Downpayment, Free Golf Membership!!!
The above url will report 400 bad request,
how to convert such title to user friendly good request?
You may want to use a "slug" instead. Rather than using the verbatim title as the URL, you strtolower() and replace all non-alphanumeric characters with hyphens, then remove duplicate hyphens. If you feel like extra credit, you can strip out stopwords, too.
So "1-As Low As 10% Downpayment, Free Golf Membership!!!" becomes:
as-low-as-10-downpayment-free-gold-membership
Something like this:
function sluggify($url)
{
# Prep string with some basic normalization
$url = strtolower($url);
$url = strip_tags($url);
$url = stripslashes($url);
$url = html_entity_decode($url);
# Remove quotes (can't, etc.)
$url = str_replace('\'', '', $url);
# Replace non-alpha numeric with hyphens
$match = '/[^a-z0-9]+/';
$replace = '-';
$url = preg_replace($match, $replace, $url);
$url = trim($url, '-');
return $url;
}
You could probably shorten it with longer regexps but it's pretty straightforward as-is. The bonus is that you can use the same function to validate the query parameter before you run a query on the database to match the title, so someone can't stick silly things into your database.
See the first answer here URL Friendly Username in PHP?:
function Slug($string)
{
return strtolower(trim(preg_replace('~[^0-9a-z]+~i', '-', html_entity_decode(preg_replace('~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i', '$1', htmlentities($string, ENT_QUOTES, 'UTF-8')), ENT_QUOTES, 'UTF-8')), '-'));
}
$user = 'Alix Axel';
echo Slug($user); // alix-axel
$user = 'Álix Ãxel';
echo Slug($user); // alix-axel
$user = 'Álix----_Ãxel!?!?';
echo Slug($user); // alix-axel
You can use urlencode or rawurlencode... for example Wikipedia do that. See this link:
http://en.wikipedia.org/wiki/Ichigo_100%25
that's the php encoding for % = %25
I just create a gist with a useful slug function:
https://gist.github.com/ninjagab/11244087
You can use it to convert title to seo friendly url.
<?php
class SanitizeUrl {
public static function slug($string, $space="-") {
$string = utf8_encode($string);
if (function_exists('iconv')) {
$string = iconv('UTF-8', 'ASCII//TRANSLIT', $string);
}
$string = preg_replace("/[^a-zA-Z0-9 \-]/", "", $string);
$string = trim(preg_replace("/\\s+/", " ", $string));
$string = strtolower($string);
$string = str_replace(" ", $space, $string);
return $string;
}
}
$title = 'Thi is a test string with some "strange" chars ò à ù...';
echo SanitizeUrl::slug($title);
//this will output:
//thi-is-a-test-string-with-some-strange-chars-o-a-u
You could use the rawurlencode() function
To simplify just full the list of the variable $change_to and $to_change
<?php
// Just full the array list to make replacement complete
// In this space will change to _, à to just a
$to_change = [
' ', 'à', 'à', 'â','é', 'è', 'ê', 'ç', 'ù', 'ô', 'ö' // and so on
];
$change_to = [
'_', 'a', 'a', 'a', 'e', 'e', 'e','c', 'u', 'o', 'o' // and so on
];
$texts = 'This is my slug in êlàb élaboré par';
$page_id = str_replace($to_change, $change_to, $texts);

Categories