How to use substr in php to ignore accented characters? - php

I am working on a php code as shown below which prints H
echo substr("Hello world",0, 1);
Now, I have an accented Ĥ in Ĥello. The following code prints �.
echo substr("Ĥello world",0, 1);
Problem Statement:
I am wondering what changes I should make in the second echo statement above with Ĥello world as the string so that it prints H instead of �.
Note: I want the second echo statement to return H not Ĥ. Only H
I have used the following code but it seems to return Ĥ not H
echo mb_substr("Ĥello world",0, 1);

Related

using fpdf : str_pad doesn't work with spaces, it works with other characters

I am adding a pad to my string, to fill with spaces, but it doesn't work
the code is here
<?php
$string1 = "Product 1 ";
$newString = str_pad($string1,100);
echo $newString."test";
echo "<br>";
$string2 = "Product 2222 ";
echo str_pad($string2,100," ")."test";
echo "<br>";
?>
the output is like this:
Product 1 test
Product 2222 test
You could try $str = str_pad($string2,(100*strlen(" "))," ")."test"; instead.
renders to a non-breaking-space in html (and when writing to document with fpdf).
Please note this can only work with fpdf when you tell it to write all lines as html! And the encoding should be utf-8 probably
$fpdf->Write(iconv('UTF-8', 'windows-1252', html_entity_decode($str)));
When the output of the PHP is converted to HTML, all the white spaces except the first are removed and it is the default feature of HTML and web browsers. so the output will not be correct.
You have to use the " " instead of white space in the str_pad function. HTML don't ignore the " " and against each existance of it, HTML adds a white space to the string.

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 );

Array element less than sign is not printing

I've got a PHP problem. When I have the following array:
$string = array('<','s');
echo $string[0];
echo $string[1];
Nothing is showing
It prints fine if I put any other special character or integer value in place of the 's'
$string = array('<','1');
echo $string[0];
echo $string[1];
output: <1
OR
$string = array('<','1#');
echo $string[0];
echo $string[1];
output: <#
I assume that your output is not being shown to you as expected because you are looking at it in a web browser. Anything starting with a < character followed by a letter is going to be interpreted as an HTML tag.
If you look at the page source of your output, you will probably see what you are looking for.
I'm sure PHP has a way to output escaped HTML tags and such out to a page, but I'm not familiar with it.
$string = array('<','s');
echo htmlentities($string[0]);
echo htmlentities($string[1]);
when you echo '<', the browser assumes you opened a tag name & expects '>'. as you know, content inside < & > is a tag & doesn't show on output(tags are used for formatting & sometimes styling the page). so, use echo htmlspecialchars($string[0]) instead.
Less than and more than-characters are typically used for defining elements in your html, therefore you must define that these characters should be output like "normal characters" instead:
< = less than (<)
> = more than (>)
In your example:
$string = array(<,'1');
Take a look at http://www.ascii.cl/htmlcodes.htm and look in the "html name column"
You could aslo have a look at http://php.net/htmlentities

How to fix PHP preg_replace() faulty result order?

The faulty result I get now is: 17th of July, 2011Today is .
function finclude($file){
include($file);
}
$str = "Today is {include 'date.php'}.";
echo preg_replace("/\{include '(.*)\'}/e", 'finclude("$1")', $str);
date.php :
<?php echo date('jS \of F'); ?>, 2011
Expected result: Today is 17th of July.
function finclude($file){
return include($file);
}
<?php return date('jS \of F'); ?>
Result isn't expected because You print date, then finclude return null, then you print "Today is "+finclude
What you call faulty in your result order is actually caused by the execution order of your statements:
echo preg_replace("/\{include '(.*)\'}/e", 'finclude("$1")', $str);
Will start the output (echo) and then call the preg_replace function. In which you make use of the e - eval modifier to execute code, namely the function finclude.
So finclude get's executed earlier than preg_replace will return it's result.
So if finclude does output on its own, it will be displayed in front of the result of preg_replace.
Knowing this is half the solution to your problem. It's much likely you didn't intend this output order (your expected result differs) and you just wanted to make finclude return a value instead of outputting something. To convert output into a return value you can make use of an output buffer:
function finclude($file){
ob_start();
include($file);
return ob_get_clean();
}
$str = "Today is {include 'date.php'}.";
echo preg_replace("/\{include '(.*)\'}/e", 'finclude("$1")', $str);
This will ensure that every output within finclude will be returned as a return value instead.
That done you can re-use existing code/includes that normally outputs within your search and replace operation. However using the e modifier always is dangerous and it normally should be prevented. So take care.
i think you need to put <?php return date('jS \of F'); ?> in date.php

str_replace just returns caps

i have a lil problem here..i'm using str_replace to replace the most common words..and for some reason its replacing every letter except caps.
for example..if i had the code below
$str ="Fat string of Text.";
$commonwords = array('fat','of','random');
$cleantext = str_replace($commonwords,'',$str);
echo $cleantext;
it would echo.. F T
any ideas what i did wrong..
thanks in advance
and oh..i tried str_ireplace.. but nothing
This echos "Fat string Text".
Your PHP installation may be wrong or your posted code that does not exactly match the program you are running
Also, str_ireplace echos "string Text".
Can't reproduce that on PHP 5.3.3. I get:
php > $str ="Fat string of Text.";
php > $commonwords = array('fat','of','random');
php > $cleantext = str_replace($commonwords,'',$str);
php > echo $cleantext;
Fat string Text.
php > $cleantext = str_ireplace($commonwords,'',$str);
php > echo $cleantext;
string Text.
as expected.

Categories