whitespace PHP link - php

I'm using PHP to send a string with $_GET to another webpage.
My main issue is when I try to see the whole string in my link just appear the first word "Im".
I'm pretty sure it is by double quotes but I'm not sure to do this.
<a href="edit.php?title=library&description=Im" going="" to="" the="" library="" tomorrow="">
I try to put this in my PHP code to replace "" character to '' but it doesn't work.
$length = strlen($_GET['description']);
$i=0;
do{
$_GET['description'][$i] = preg_replace('/["]/', '', $_GET['description][$i]);
$i++;
}while($i < $length);
My PHP link is the next:
echo "<a href=edit.php?title=$_GET[title]&description=$_GET[description]>";
If anyone knows what's going on, please help me.

Have you tried using the rawurlencode function?
// if $_GET["title"] = "Im going to the library tomorrow" ...
echo "<a href=edit.php?title=" . rawurlencode($_GET["title"]) . "&description=" . rawurlencode($_GET["description"]) . ">";
You may need to use rawurldecode when you receive this.

Related

PHP string replace with characters inbetween tags

I need help with my PHP, I'm using str_ireplace() and I want to filter something out and replace it with what I have.
I find it hard to explain what I am talking about so I will give an example below:
This is what I need
$string = "<error> " . md5(rand(0, 1000)) . time() . " </error> Test:)";
then I want to remove and replace the whole <error> .... </error> with nothing.
So the end outcome should just print 'Test:)'.
Your question is not perfectly clear, but I believe I may understand what you are asking. This code may do the trick:
$string = " " . md5(rand(0, 1000)) . time() . " Test:)";
$newstring = preg_replace("/.*?\ /i", "", $string);
This uses regular expressions to filter out everything that comes before the space (and also removes the space)

escape character not working as expected

I am trying to create html content in PHP and for onclick event I have included a function named uchat for a div. The function takes a name parameter which is a string.
Like below:
$name = "Php string";
$echostr .= "<div onClick='uchat(\'$name\')'>
</div>";
But, passing a string value like this causes syntax error when div is clicked. Because, single quote is within a single quote. I have tried to escape it, but it still doesnt work.
The error is this:
SyntaxError: illegal character
uchat(\
I am not sure how to escape a string parameter and I have come across this problem so many times, Please help if you have a solution for this.
Thanks.
Escaped single quotes will conflict with outer ones:
$echostr .= "<div onClick=\"uchat('$name')\">
</div>";
Here are 2 clean and simple ways to do this:
1. Classic concat
$name = "Php string";
$str = "<div onClick=\"uchat('" . $name . "')\"></div>";
print $str;
2. Using sprintf (http://us3.php.net/manual/en/function.sprintf.php)
$name = "Php string2";
$str = sprintf("<div onClick=\"uchat('%s')\"></div>", $name);
print $str;
try like this
$echostr .= "<div onClick='uchat("$name")'></div>";
This works:
<?php
$name = "Php string";
$echostr .= <<< EOF
<div onClick="uchat('$name')"></div>
EOF;
echo $echostr;
?>
Output:
<div onClick="uchat('Php string')"></div>
In order to avoid escaping all double quotes and to make the html code more readable you can use EOF.
See it action : http://ideone.com/vRCCVH

printing a php variable as it is : with all the special characters

Ok I need to find out what is contained inside a PHP variable and I have it to do it visually, is there a function to display whatever that's contained in a string as it is?
For example :
$TEST = '&nbsp' . "\n" . ' ';
if I use echo the output will be :
while i want it to be :
&nbsp\n&nbsp
is it possible? (I hope I was clear enough)
ty
You can use json_encode with htmlspecialchars:
$TEST = ' ' . "\n" . ' ';
echo json_encode(htmlspecialchars($TEST));
Note that json_encode has third agrument in PHP 5.4.
var_dump() should do the work for you?
Example:
echo "<pre>";
var_dump($variable);
echo "</pre>";
Use <pre> to keep the format structure, makes it alot easier to read.
Resources:
http://php.net/manual/en/function.var-dump.php
http://www.w3schools.com/tags/tag_pre.asp
Try print_r, var_dump or var_export functions, you'll find them very handy for this kind of needs!
http://www.php.net/manual/en/function.htmlspecialchars.php
or
http://www.php.net/manual/en/function.htmlentities.php
$TEST = '&nbsp' . "\n" . ' ';
echo htmlspecialchars(str_replace('\n','\\n', $TEST), ENT_QUOTES);
or
$TEST = '&nbsp' . "\n" . ' ';
echo htmlentities(str_replace('\n','\\n',$TEST), ENT_QUOTES);
You may have to encode the newlines manually. If you want to encode them as actual newlines you can use nl2br. Or string replace these characters with your preference. Update: as I have added to the code per request. String replace special characters you wish to see like newlines and tabs.
assuming you want it for the debugging purposes, let me suggest to use urlencode(). I am using it to make sure I don't miss any invisible character.
The output is not that clear but it works for me.

A simple question in PHP but need help

To percent encode a input string (an XML file), only for % and line terminators..
Don't roll your own URL encoding. Use the built-in stuff.
$xml = urlencode($xml);
If I understood your question correctly, you'll need to do something like
$input .= 'datacenter: ' . str_replace(array('\n', '\r'), array('%0A', '%0D'), $xmlfile) . "\n";

How do I auto convert an url into a hyper link in PHP?

I have a script that outputs status updates and I need to write a script that automatically changes something like www.example.com into a hyper link in a chunk of text like Twitter and Facebook do. What functions can I use for this in PHP? If you know a tutorial please post it.
$string = " fasfasd http://webarto.com fasfsafa";
echo preg_replace("#http://([\S]+?)#Uis", '<a rel="nofollow" href="http://\\1">\\1</a>', $string);
Output:
fasfasd <a rel="nofollow" href="http://webarto.com">webarto.com</a> fasfsafa
You can use a regex to replace the url with a link. Look at the answers on this thread: PHP - Add link to a URL in a string.
Great solution!
I wanted to auto-link web links and also to truncate the displayed URL text, because long URLs were breaking out of the layout on some platforms.
After much fiddling around with regex, I realised the solution is actually CSS - this site gives a simple solution using CSS white-space.
Here is the working Function
function AutoLinkUrls($str,$popup = FALSE){
if (preg_match_all("#(^|\s|\()((http(s?)://)|(www\.))(\w+[^\s\)\<]+)#i", $str, $matches)){
$pop = ($popup == TRUE) ? " target=\"_blank\" " : "";
for ($i = 0; $i < count($matches['0']); $i++){
$period = '';
if (preg_match("|\.$|", $matches['6'][$i])){
$period = '.';
$matches['6'][$i] = substr($matches['6'][$i], 0, -1);
}
$str = str_replace($matches['0'][$i],
$matches['1'][$i].'</xmp><a href="http'.
$matches['4'][$i].'://'.
$matches['5'][$i].
$matches['6'][$i].'"'.$pop.'>http'.
$matches['4'][$i].'://'.
$matches['5'][$i].
$matches['6'][$i].'</a><xmp>'.
$period, $str);
}//end for
}//end if
return $str; }

Categories