"\u00e1n" to "á" in PHP [closed] - php

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 8 years ago.
Improve this question
I am receiving an external file with strings, I have no control over that file but I receive special characters as this (i think it's unicode?) sequence \u00e1n.
Is there anyway to convert this kind of char sequences to their "readable" counterparts?
Thanks!
EDIT:
I am calling a url that gives me a list with names of persons:
Tom\u00e1nsson\n
Eriksen\n
Gilverto\n
I am reading the names and showing them in my site.

EDIT
Now, I've figured out the correct, working answer based on the article I found. Here is it:
function unicode2utf8($str) {
$a=json_decode(str_replace("\n","<br>",'["'.$str.'"]'));
return str_replace("<br>","\n",$a[0]);
}
PHP:
echo unicode2utf8("Tom\u00e1nsson\n Eriksen\n Gilverto\n");
Output:
Tománsson
Eriksen
Gilverto
ORIGINAL
I've found an article about the same problem here ( http://www.welefen.com/php-unicode-to-utf8.html )
The solution is the following function
function unicode2utf8($str){
if(!$str) return $str;
$decode = json_decode($str);
if($decode) return $decode;
$str = '["' . $str . '"]';
$decode = json_decode($str);
if(count($decode) == 1){
return $decode[0];
}
return $str;
}
I've tried it with
echo unicode2utf8("\u00e1");
Output:
á

Related

I cannot decode or extract any information from this string [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 3 years ago.
Improve this question
How to decode the following string so I can extract readable data from it:
"H4sIAAAAAAAAAEWRz3LaMBDG…HXHYhZxEsNvDIH3B4ACAAA="
The only thing that I know about this string is that it may be random generated bytes, that's all I know, unfortunately.
I tried base64_decode() but that didn't work it just gave me this string:
?E??r?0?q??D??? }???
echo base64_decode("H4sIAAAAAAAAAEWRz3LaMBDG…HXHYhZxEsNvDIH3B4ACAAA=");
This string should give me really useful information for the API project I'm working on, also if there is an alternative way to do this using javascript please let me know, Thanks.
I have searched for H4sIAAA and found gzdecode() which decoded this string, thank you for your help Pavan Kumar.
$string = "H4sIAAAAAAAAABWPQU+DQBCFH7RVICaeTDyZ7cGrB5PGM4XVNhLiofXaTGGkG2Fp2MXIL+J/8MOM27m9b95M3ouAEJ6KAHg+fFV6bx4WSdtr60WYWapCzFkXJ1xmhnCjSn6tqTJO/kW4LpU51zQ4V9Z2HDi6wMM0vmx10TEZNsKeWJgzcynaLzG0fYel2zdKq1aL4yCmkZ5Xjw49ib2uVaMsl7hxOu07ss60dE+jaVxNYy0/tkmAeU4N494h6ZKRdgciox8S6774ZosIt/LXdhRb26ljb9kEl2q4k3myifOdTA9Z/Bkf1vvkXe4AH1cpNVTxpeM/Fr19kRIBAAA=";
echo gzdecode(base64_decode($string))
//Result i idGCount tag ench HideFlags� display Lore §7Increases the speed of your!§7minion by §a25%§7. Unlimited§7Duration! §5§lEPICName§5Enchanted Lava Bucket ExtraAttributesidENCHANTED_LAVA_BUCKETDamage
You can get quite close using the following:
function inspect( $s ){
$b=rawurldecode( base64_decode( trim( $s ) ) );
$g=gzdecode( $b );
return sprintf('<pre>%s</pre>',print_r($g,1));
}
$i=0;
$max=10;
$url='https://api.hypixel.net/skyblock/auctions?key=09828659-42c5-4360-9203-d93bcb5df79d';
$data=file_get_contents( $url );
$json=json_decode( $data );
foreach( $json->auctions as $obj ){
echo inspect( $obj->item_bytes );
if( $i >= $max )break;
$i++;
}
sample output:
i
idGCount
tag ench HideFlags�
display Lore§7Increases the speed of your!§7minion by §a25%§7. Unlimited§7Duration!
§5§lEPICName§5Enchanted Lava Bucket
ExtraAttributesidENCHANTED_LAVA_BUCKETDamage

How to match div content which has divs inside by using regex [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 7 years ago.
Improve this question
Content is
<html>...<div id="endText" class="end-text" jcid="8311"><p>Hello</p>World<div class="ep-source cDGray"></div></div>...</html>
How to match
<p>Hello</p>World<div class="ep-source cDGray"></div>
Thank you!
#Rizier123
$content = '';
if(preg_match('/"endText".+?>.+?(?=<div.+?class="ep-source cDGray">)/i', $html, $contents) &&
preg_match('/(?<=>).+/i', $contents[0], $contentss))
{
$content = iconv('GBK', 'UTF-8', $contentss[0]);
return rtrim('OK' . "\t" . $content);
}
else
{
return rtrim('SKIP' . "\t" . 'NO_CONTENT');
}
This method can be used temporary, can't solve the problem.
Just match and remove the first-level div.
Regex (matches the opening div, saves its innards, and matches the last closing div):
/^<div id="endText"[^>]+>(.*?)<\/div>$/ism
PHP example:
preg_match('/^<div id="endText"[^>]+>(.*?)<\/div>$/ism', $html, $contents);
echo $contents[1];
// returns: <p>Hello</p>World<div class="ep-source cDGray"></div>
Adding the id attribute to the regex helps to specify that particular div

How can I get input fields names from a string with regex? [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 8 years ago.
Improve this question
I have some form in a variable. I have some input fields with names. How can I get all name="" from that string?
Now I'm trying this:
preg_match_all ('/(name="(.*?"))\d/s', $ki, $matches1);
But not working well. It gives me just a few and it gives me a lots of unnececary informations.
Try with this regex:
preg_match_all( '/name\s*=\s*(?|"(.*?)"|\'(.*?)\')/', $ki, $matches1 );
You can also do this using array.
i.e. By using explode method
$content='name="name1" other example text name="name2" other example text name="name3" ';
$finalResult=array();
function getBetweenAll($content,$start,$end)
{
$res = explode($start , $content , 2);
if(isset($res[1]) && $res[1]!='' )
{
$value = explode($end , $res[1] , 2);
global $finalResult;
$finalResult[]=$value[0];
if(isset($value[1]) && $value[1]!='')
{
$content=$value[1];
getBetweenAll($content,$start,$end);
}
}
}
$start='name="';
$end='"';
getBetweenAll($content,$start,$end);
echo "<pre>";
print_r($finalResult);
echo "</pre>";

How to separate text with PHP [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 8 years ago.
Improve this question
Well I am trying to make a simple php system.
Anywise I need to separate the text when I want to add it to the database.
So for example I want to add:
abc:123
I want that the : will be the separater, so it'll look like this:
abc
123
And then both will go to a different table.
Could someone help me with this? As I am not an experience PHP coder, yet I am willing to learn how to do this.
Kind regards
This is pretty basic stuff..
$data = explode(':','abc:123');
foreach($data as $word)
{
// some code here
}
Use Split:
<?php
$data = "abc:123";
list ($var1, $var2) = split (':', $data);
echo "Var1: $var1; Var2: $var2;<br />\n";
?>
You can achieve this using explode.
abc:123
Is a string. Let's define it as a variable:
$origin = "abc:123";
You can split the string, using : as the separator.
$separator = ":";
$exploded = explode($separator, $origin);
Now you have an array which you can use to access abc and 123 individually.
$pre = $exploded[0];
$post = $exploded[1];
You don't know how many splits there will be?
That's okay. Your array simply increases, meaning you can simply loop through the array and handle the values.
foreach ($exploded as $split)
{
// Do something with $split
}

How to extract the value in double quotation? [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 8 years ago.
Improve this question
I need to extract the value of the attributes of a html tag that is defined as a php variable:
[video src="http://localhost/video.mp4" poster="http://localhost/thumb.jpg" preload="none"][/video]
values ​​of src and poster should be separated and cast to array
if your text is a php variable, you can use preg_match :
<?php
$text = '[video src="http://localhost/video.mp4" poster="http://localhost/thumb.jpg" preload="none"][/video]';
preg_match('/src=\"([^\"]+)\"/si', $text, $src);
preg_match('/poster=\"([^\"]+)\"/si', $text, $poster);
$array['src'] = $src[1];
$array['poster'] = $poster[1];
print_r($array);
?>
also you can use simplehtmldom
You can use an html parser for this. See this one: http://simplehtmldom.sourceforge.net/.
$html = str_get_html('<video src="http://localhost/video.mp4" poster="http://localhost/thumb.jpg" preload="none"></video>');
foreach($html->find('video') as $element)
{
$array['src'] = $element->src;
$array['poster'] = $element->poster;
}

Categories