How to display part of string if string contains special character - php

I have following line of code,
echo substr('Sergio Agüero',0,10);
And it will display Sergio Ag�
But I want output like "Sergio Agü"
I don't want special character. So is it possible? Any help is really appreciated.

You can do this using mb_internal_encoding and mb_substr.
Example: online test
mb_internal_encoding("UTF-8");
$str = 'Sergio Agüero';
echo mb_substr($str, 0, 10); //Sergio Agü
More about: mb-substr

Use utf8_decode. Try this:
echo substr(utf8_decode("Sergio Agüero"),0,10);

Related

Substr String With HTML In PHP

I have some text that comes back from my database like so:
<span rgb(61,="" 36,="" 36);="" font-family:="" 'frutiger="" neue="" w01="" book',="" 'helvetica="" neue',="" helvetica,="" arial,="" sans-serif;="" line-height:="" 23.8px;"="">The Department of ...
I use echo html_entity_decode($item->body); to display:
The Department of ...
However, if I use the PHP substr function on this content it never displays correctly. It will display the first x characters of HTML and not the HTML formatted text.
Here's what I tried: echo substr(html_entity_decode($item->body), 0, 5);
But it doesn't display anything. If I try an amount like 0, 200); it will display:
The Department of Molec
But this is most definitely not the first 200 characters of the formatted text because the first character is T.
My idea is that there must be way to format and then substr, even though I can't get it to work using html_entity_decode() and substr() by themselves.
Can anybody help me out here? Thanks!
Try to use this instead of html_entity_decode():
strip_tags($item->body);
strip_tags removes all HTML tags from the string. So you better of treating the string and then do something with it.
You will see the output in the source code, but it is not beeing rendered. The source code will show:
echo substr(html_entity_decode($item->body), 0, 5);
// Output: "<span"
What you probably want to do is search for the end of the html-tag, and display 5 characters after that, like:
$text = html_entity_decode($item->body);
$start = strpos( $text, '>' ) + 1;
echo substr( $text, $start, 5 );

How to make file_get_contents recognize escape sequences as special characters?

I need to get content from a file so that the escape sequences (like \n) got recognized as special characters.
Consider the code:
<?php
$f = file_get_contents("test.txt");
echo "$f";
?>
while test.txt contains only:
Test\nOnly
It echoes:
Test\nOnly
while I'd like to have:
Test
Only
Is there a way to accomplish it with file_get_content or should I use something else (like output buffering)?
try using printf which the function outputs a formatted string.
<?php
$f = file_get_contents("test.txt");
printf ($f);
?>

Why is this not making a plus sign?

I have:
$str = 'test%2B';
echo html_entity_decode($str);
I want it to return test +
What am I doing wrong?
NOTE: Sorry, the string cannot be modified. It's from an external source, I just need to make it replace the %2B with + signs somehow with PHP.
You didn't escape the space, and you should be using urldecode instead of html_entity_decode.
Try
$str = 'test%20%2B';
echo urldecode($str); // test +
If you wish to use html_entity_decode, use +:
$str = 'test +';
echo html_entity_decode($str); // test +
EDIT: If you need to decode a url that you cannot change yourself, urldecode should still work fine.
That string is encoded for a URL, not with HTML entities.
You need urldecode.
echo urldecode($str); // "test +"
An HTML-encoded string would look like this: test +, because none of those characters need HTML-encoding.
Try + instead. In your example, you are using URL encoding syntax and not HTML entity syntax.
In html a + is +. Try
$str = 'test +';
$str = "test %2B";
echo urldecode($str);

Problem in UTF Encoding in PHP

I use the following lines of code:
$revTerm = "". strrev($limitAry["term"]);
$revTerm = utf8_encode($revTerm);
The $revTerm contains Norwegian characters as ø æ å. However, it is shown correctly. I need to reverse them before displaying, so I use the first line.
When I display them this way, I get an error of bad xml format - used to fill a grid.
When I try to use the second line, I don't get an error but the characters are not shown correctly. Could there be any other way to solve that?
If it may help, I use jqGrid to fill those data in.
strrev, like most PHP string functions, is not safe for multi-byte encodings.
try this example
$test = 'А роза упала на лапу Азора ウィキ';
$test = iconv('utf-8', 'utf-16le', $test);
$test = strrev($test);
// キィウ арозА упал ан алапу азор А
echo iconv('utf-16be', 'utf-8', $test);
(russian)
http://bolknote.ru/2012/04/02/~3625#56
Try this:
$revTerm = utf8_decode($limitAry["term"]);
$revTerm = strrev($revTerm);
$revTerm = utf8_encode($revTerm);
For using strrev you have to decode your string to a non-multibyte string.

PHP - convert a string with - or + signs to HTML

How do I convert a string that has a - or + sign to a html friendly string?
I mean to convert those characters to html notations, like space is and so on...
ps: htmlentities doesn't work. I still see the -/+
Try this
$string = str_replace('+', '+', $string); // Convert + sign
$string = str_replace('-', '-', $string); // Convert - sign
I don't think there is entities for these symbols see: http://www.w3schools.com/tags/ref_entities.asp
I tested with
$str = "- and +"; echo htmlentities($str);
and didn't get entities. According to: http://us.php.net/manual/en/function.htmlentities.php
I would expect them to be encoded if there was encoding available.
No idea what you want to accomplish. But this escapes selected characters to html entities:
$html = preg_replace("/([+-])/e", '"&#".ord("$1").";"', $html);
As far as I am aware, - and + are fine in HTML, and dont have an entity equivalent. See http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references
Are you sure you're not thinking of URL encoding?
Specify that you want it to use unicode as follows:
htmlentities($str, ENT_QUOTES | ENT_IGNORE, "UTF-8");
Have a look at the 2nd comment on this page:
http://www.php.net/manual/en/function.htmlentities.php#100388
This will enable more encoding characters.
If you just want to encode some, then this is a little lighter weight:
<?php
$ent = array(
'+'=>'+',
'-'=>'+'
);
echo strtr('+ and -', $ent);
?>

Categories