Steam API str_replace htmlspecialchars - php

i have been making a Steam API. However i need remove htmlspecialchars from Steam Name when they login.
This is what i have done so far:
Get user name from database:
$name = fetchinfo("name","users","steamid",$steamid);
When they login it should str_replace his name:
$name = str_replace("**something here**",'',$name);
How can i add htmlspecialchars for that?

It's easy enough to run both str_replace and htmlspecialchars on your string through nesting:
$name = "Tony & Sarah";
$name = htmlspecialchars( str_replace(' ','-',$name) );
// $name == "Tony-&-Sarah"
If this doesn't match what you're looking for, an example of what you're trying to do with str_replace would be helpful.

Use preg_replace() for this:
function replaceSpecialChars($string, $chars){
$c = implode('\\', str_split($chars));
return preg_replace('/['.$c.']/', '', $string);
}
$test = replaceSpecialChars('a##bcde %8&)`', '`~!##$%^&*()');

Related

Ignore specific character: -

I want to ignore a specific character using php. So when a user adds this character in the textbox. the php scripts filters it out first. I tried something and came up with this:
<?php
$datetogoto = $_GET['datetogoto'];
$pattern = '-';
$replace = '';
preg_replace($pattern, $replace, $datetogoto);
header('Location: ../index.php?newsdate='.$datetogoto);
?>
So what it wrong with this code?
Can you try using str_replace
$datetogoto = $_GET['datetogoto'];
$datetogoto = str_replace("-","", $datetogoto);
Ref: http://us1.php.net/str_replace
Or , if you want get date format whatever you sent in query string, then use urlencode()
header('Location: ../index.php?newsdate='.urlencode($datetogoto));
PHP regex needs delimiters, so use it like this:
$pattern = '/-/';
OR else use str_replace:
str_replace('-', $replace, $datetogoto);

How to remove links from a PHP string using preg_replace

I'm using a chat bot script, if a user name was test#test.com the bot will reply # <a href= mailto:test#test.com>test#test.com</a> with a mailto link. I want the reply to be only test#test.com without the link, I tried preg_replace and str_replace but I don't really know the exact code to use, I've tried the following but didnt work !
$name = preg_replace('/<a href="([^<]*)">([^<]*)<\/a>/', '', $name);
The whole code I'm using for replacements is this:
$name = str_replace (chr(0xc2).chr(0xa0), "_", $name);
$name = str_replace ("'", "", $name);
$name = str_replace (""", '"', $name);
$name = str_replace ("&", "&", $name);
$name = str_replace ("<", "", $name);
$name = str_replace (">", "", $name);
$name = str_replace ("&", "_", $name);
$name = str_replace ("*", "_", $name);
$name = preg_replace('/[^ \p{L}\p{N} \# \_ \- \.\#\$\&\!]/u', '', $name);
$name = preg_replace('/<a href="([^<]*)">([^<]*)<\/a>/', '', $name);
Why do you want to replace it? Just use preg_match() with a regex similar to this:
<a href=[^>]+>([^<]*)</a>
so overall your code would look like this
<?php
$regex = '#<a href=[^>]+>([^<]*)</a>#';
$email = '<a href= mailto:test#test.com>test#test.com</a>';
preg_match($regex, $email, $matches);
var_dump($matches[1]);
/*
output:
string(13) "test#test.com"
*/
?>
The answer above makes a lot of assumptions when doing the preg_replace so it's going to fail lots unfortunately :( Here's why...
It assumes every link has the 'href' attribute directly after the 'a' tag. What if there's a different attribute in front of it?
It assumes there are no other html tags inside the 'a' tag. If the link had the 'strong' tag inside it, the link would not be matched.
I'm pretty sure too that if there's more than one link in the list it's going to remove everything between the first link and the second because it hasn't got anything to stop it being greedy.
Finally, it's not been told to be insensitive. This means that if the link had A HREF in it, that wouldn't be found either.
I'm not saying my solution is 100% secure but I've tested it in scenarios I'm aware of and I think it's an upgrade from the answer above!...
$email = preg_replace("/<a.+?href.+?>.+?<\/a>/is","",$email);
The 'i' modifier makes it insensitive
The 's' modifier takes into account links that might be broken with newline breaks.
I'd always recommend populating a string with different links in different formats, different orders etc. That's always the best way to test things work. Assuming eveyone types links as My test is going to get you into lots of sticky situations :)
Good luck!

list of all PHP preg_replace characters to escape

Where can find a list of all characters that must be escaped when using preg_replace. I listed what I think are three of them in the array $ESCAPE_CHARS. What other ones am I missing.
I need this because I am going to be doing a preg replace on a form submission.
So ie.
$ESCAPE_CHARS = array("#", "^", "[");
foreach ($ESCAPE_CHARS as $char) {
$_POST{"string"} = str_replace("$char", "\\$char", $_POST{"string"});
}
$string = $_POST{"string"};
$test = "string of text";
$test = preg_replace("$string", "<b>$string</b>", $test);
Thanks!
You can use preg_quote():
$keywords = '$40 for a g3/400';
$keywords = preg_quote($keywords, '/');
print $keywords;
// \$40 for a g3\/400

Removing with PHP after CURL request

I get text from a website via CURL however it has on the end on the original website, however when I return it back to mine, this is just a space(after looking at it in the source), however nothing seems to help even with str_replace, (string), strval() and so on.
Could anyone help?
Current code
$country = $s->fetchBetween('<dt>Country:</dt><dd>','</dd>', $result);
$country = strval($country);
$country = str_replace(" ", "", strval($country));
echo "start-{$country}-end";
exit;
The value on the website that I'm "Curling" appears as
India
Try the following instead.
$country = str_replace("&nbsp;", "", strval($country));
Try this out.
$string = 'x x ';
$pattern = '/ /';
echo preg_replace($pattern, '', $string);

How to convert a string to an integer?

<?php
header('Content-type: text/html; charset=gb2312');
include("parser.php");
$html = file_get_html('http://www.souyishop.com/shop_gkq5/1280/products.aspx?sku=1351249&shbid=20358');
$price = $html->find('span[id=Product_main1_Label5]');
$price = str_replace(" ", "", str_replace("¥", "", str_replace("元", "", $price[0])));
echo (int)$price . "<br>";
?>
Basically, I'm trying to fetch the price ¥ 32 元 and I remove the currency and white space. After that I try to convert the string to int so that I can do calculation later. Guess what, I get 0. :(
Somethign like this isnt going to work:
echo intval('¥ 32');
So try replacing all nondigits
echo (int)preg_replace( '~\D~', '', $str );
Use a regular expression in this case:
$re = preg_match('/(\d+)/', $str, $matches);
$price = $matches[1];
That str_replace chain is cumbersome
A more elegant solution is to pull out all numbers with preg_match
$price = preg_match('/[0-9]+/i', $price, $matches);
print_r($matches);
I think you should first echo $price without and int conversion to determine if you are actually replacing the string items you think you are, this is probably why it returns 0.
Chinese encoding requires special handling.
edit:
try using the html code for the char (yen = ¥) see: http://www.starr.net/is/type/htmlcodes.html

Categories