Showing problems in '<' and '>' symbols in php - php

I have a string
$string = "test<String>";
but when i make
echo $string;
it shows:
testString
and when I send the $string to mobile as SMS it shows
<testString>
How can I show "test<String>" with < and > symbol ?
thanks in advance.

You basically do it in reverse.
<String>
this will tell html to output the less than and greater than characters.
This should help you out
http://www.w3schools.com/html/html_entities.asp

You can try:
$string = "test<string>";
$string = htmlspecialchars($string);
echo $string;

Don't know what SMS-api you are using, maybe you have to urlencode the string before you send it to the api.

Didn't find any solution for mobile sms tag, so for mobile i've used ( and ) instead of < and > for serving purpose temporarily.

Related

Text between less than and greater than not output PHP

i use (str_replace) function to replace ##ID## in youtube url with this regular expression : (?P<id>[a-z-A-Z_0-9]+)
so i use this code to do this :
<?php
$urlbase = 'https://www.youtube.com/watch?v=##ID##';
$lastchange = str_replace('##ID##', '(<id>[a-z-A-Z_0-9]+)', $urlbase);
echo $lastchange;
?>
i get the output in the browser like this : https://www.youtube.com/watch?v=(?P[a-z-A-Z_0-9]+), its looks like <id> not show up !
i try this simple code :
<?php
echo "This is my <id>";
?>
but i just get this is my in the browser !
What's the probleme ? and how i can fix it , thanks
is being interpreted as HTML so your browser is parsing it and since it is not a renderable element, it shows nothing. Try:
<?php
echo "This is my <id>
?>
As for the str_replace, it's doing exactly what the function is supposed to be doing. If you're looking to use regular expressions in string replacements, use preg_replace
The tag <id> is being removed by your browser. It is really there if you watch the source code. Maybe you should try:
$urlbase = 'https://www.youtube.com/watch?v=##ID##';
$lastchange = str_replace('##ID##', '(<id>[a-z-A-Z_0-9]+)', $urlbase);
echo urlencode( $lastchange );
Problem is with the line:
$lastchange = str_replace('##ID##', '(<id>[a-z-A-Z_0-9]+)', $urlbase);
str_replace does not use regex.
You will need preg_replace
$pattern = '(<id>[a-z-A-Z_0-9]+)'
$replacement = '##ID##'
$string = $urlbase
$lastchange = preg_replace($pattern, $replacement, $string);
Also < and > are html entities which means they are reserved chars for HTML they have some special meanings if you want to show them then you must use there entity name eg < and > in your case respectively.
<?php
echo " echo "This is my <id>";
?>

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 can i allow only arabic characters in input text field?

I already searched here and found similar posts related to this post but i did not find a solution yet .
i tried this :
$text = "الحمد لله رب العالمين hello";
echo $is_arabic = preg_match('/\p{Arabic}/u', $text);
I add the unicode flag but if i add any English characters it is returning true ! any fix for this ?
Any idea folks ?
Thanks in advance
Use unicode flag:
$text = "الحمد لله رب العالمين";
echo $is_arabic = preg_match('/\p{Arabic}/u', $text);
here __^
If you want to match only arabic you should do:
echo $is_arabic = preg_match('/^[\s\p{Arabic}]+$/u', $text);
Update: I see I am apparently wrong about classes not being supported (though the docs at say "Extended properties such as "Greek" or "InMusicalSymbols" are not supported by PCRE" but the comment at http://php.net/manual/en/regexp.reference.unicode.php#102756 says they are supported), so I guess M42's is the better answer. They can, however, be done with ranges as follows:
$text = "الحمد لله رب العالمين";
echo $is_arabic =
preg_match('/^[\s\x{0600}-\x{06FF}\x{0750}-\x{077F}\x{08A0}-\x{08FF}\x{FB50}-\x{FDFF}\x{FE70}-\x{FEFF}\x{10E60}\x{10E60}—\x{10E7F}\x{1EE00}—\x{1EEFF}]+$/u', $text);

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

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