Adding + between keywords PHP - php

In my tag cloud I have urls that look like this
../search.php?query=cat&select=all
../search.php?query=the cat&select=all
What I am trying to do is if the keyword is more than one word like so:
../search.php?query=the cat&select=all
Put a + between the words like this
../search.php?query=the+cat&select=all
My default search function uses the + in the searches performed by the form but I would also like my tag cloud to have the same thing. I am only doing this for looks. It works perfectly fine as is but I thought I would ask anyway. Can anyone help? this is what I am currently using for links in my tag cloud
echo " <a href='../search.php?query=$word&select=all' target='rel' title='".($word)."'>".($word)."</a>";
Thanks.

I think what you're looking for is the urlencode() function.

urlencode & urldecode is just the function you require.
echo " <a href='../search.php?query=".urlencode($word)."&select=all' target='rel' title='".($word)."'>".($word)."</a>";
and on the other page you can have urldecode
echo urldecode($_GET['query']);

Related

How to use unicode in CodeIgniter?

I am using CI framework for my project. If I use like this
$title= "प्रदेश"; // प्रदेश is written in nepali langauge.
echo $title; // It will display प्रदेश
Now I want to use
<a href = "<?php echo base_url("home/$title")?>"> //home is a controller
Now In home controller if I tried to display$title it will not show प्रदेश.
It will display like this %E0%A4%AA%E0%A5%8D%E0%A4%B0%E0%A4%A6%E0%A5%87%E0%A4%B6
Please help me.
The base_url calls urlencode on the string, which makes it into url entities.
A space becomes %20 and things like that.
The url that is displayed is actually correct and should be interpretted correctly by the server, even if it's gibberish according to humans.
If you really want the human readable characters for some reason you can do two approaches, urldecode the resulting url(not recommended):
echo urldecode(base_url("home/$title"));
or
echo base_url("home/").$title

Convert Plain Text Address to clickable link to google maps via PHP

Pulling in data from a Filemaker Pro database field and trying to convert the plain text data from the field into a clickable link to google maps via PHP.
My first attempt doesnt display anything when called:
$Venue = '';
$Venue is then echoed into a UL via
<?php echo $Venue; ?>
I'm relatively new to PHP so I'm sure there is a much more semantic way of marking this up? Possibly a regex and replace, returning a preg_replace? Which is what I've been using for plain text URLs and email addresses.
Anything helps, thanks so much.
Casey - not sure there's enough context here to help you? For instance, forget the link wrapping, does
<?php echo $record->getField( 'Auctions::AIS_Venue'); ?>
actually echo the field contents to a page?
Also, not sure you really want to use nl2br anyway, as you probably don't want a break in your url ;-)
OK, in which case there are a couple of things to try. First - can you get it to run in a webviewer inside FileMaker? I suspect not, and it may be down to an encoding/filtering issue.
There are two approaches you could use.
Create a new calc field in FileMaker that filters out punctuation and converts spaces to '+'s, something like:
Filter( Substitute( Lower( Auctions::AIS_Venue ) ; [ ¶ ; "+"] ; [" " ; "+"]; ["++" ; "+"] ); "abcdefghijklmnopqrstuvwxyz1234567890+" )
and then use that field as in your PHP code, or;
Do the same calculation on the fly in PHP, something like:
$Venue = '';
That should do the trick ? ;-)

Handling %22 in urls

I have a page that uses an input field to search, and then uses that same field to go across pages. It also accepts double quotes for exact searching.
The url needs to look like blahblah.com/search/%22querystuff%22, but it autodefaults to blahblah.com/search/"querystuff" which fails in the browser.
Is there any way to get it to stop doing that or do I need to look into a different method.
Try out the urlencode method.
Here is an example of how to use it:
<?php
$userinput = '"Hello world"';
echo '<a href="http://blahblah.com/search/', urlencode($userinput), '">';
?>

Replace Specifc Full Links Between href=" " Using PHP

I have tried searching through related answers but can't quite find something that is suitable for my specific needs. I have quite a few affiliate links within 1,000s of articles on one of my wordpress sites - which all start with the same url format and sub-domain structure:
http://affiliateprogram.affiliates.com/
However, after the initial url format, the query string appended changes for each individual url in order to send visitors to specific pages on the destination site.
I am looking for something that will scan a string of html code (the article body) for all href links that include the specific domain above and then replace THE WHOLE LINK (whatever the query string appended) with another standard link of my choice.
href="http://affiliateprogram.affiliates.com/?random=query_string&page=destination"
gets replaced with
href="http://www.mylink.com"
I would ideally like to do this via php as I have a basic grasp, but if you have any other suggestions I would appreciate all input.
Thanks in advance.
<?php
$html = 'href="http://affiliateprogram.affiliates.com/?random=query_string&page=destination"';
echo preg_replace('#http://affiliateprogram.affiliates.com/([^"]+)#is', 'http://www.mylink.com', $html);
?>
http://ideone.com/qaEEM
Use a regular expression such as:
href="(https?:\/\/affiliateprogram.affiliates.com\/[^"]*)"
$data =<<<EOT
bar
foo
<a name="zz" href="http://affiliateprogram.affiliates.com/?query=random&page=destination&string">baz</a>
EOT;
echo (
preg_replace (
'#href="(https?://affiliateprogram.affiliates.com/[^"]*)"#i',
'href="http://www.mylink.com"',
$data
)
);
output
bar
foo
<a name="zz" href="http://www.mylink.com">baz</a>
$a = '<a class="***" href="http://affiliateprogram.affiliates.com/?random=query_string&page=destination" attr="***">';
$b = preg_replace("/<a([^>]*)href=\"http:\/\/affiliateprogram\.affiliates\.com\/[^\"]*\"([^>]*)>/", "<a\\1href=\"http://www.mylink.com/\"\\2>", $a);
var_dump($b); // <a class="***" href="http://www.mylink.com/" attr="***">
That's quite simple, as you only need a single placeholder for the querystring. .*? would normally do, but you can make it more specific by matching anything that's not a double quote:
$html =
preg_replace('~ href="http://affiliateprogram\.affiliates\.com/[^"]*"~i',
' href="http://www.mylink.com"', $html);
People will probably come around and recomend a longwinded domdocument approach, but that's likely overkill for such a task.

Text Display in PHP

if i stored data in DB which contains urls (for example : Go thorugh this link http://www.google.com).
when i display that data in browser, i want to display that data like " Go through this link http://www.google.com ". but that url which looks like anchor link...
if you didn't get this..open google chat...send some msg to anyone like http://google.com..if u send plain text like http://google.com,but it shows with hyper link..to that url..
i want this functionality in PHP technology...how can we implement this
thanks in advance...
So, you want to convert the urls to links in php? See the first result, or answers to same question in stackoverflow.
If I understood this correctly you want to transform URLs in a text to links automatically, without going further into details a crude (very crude) regexp should do it for now:
$textWithLinks = preg_replace('#(http|ftp)s?://[^\s]+#i', '$0', $textWithUrls);
function add_href ($text) {
return preg_replace('/((www\.|(http|https|ftp|news|file)+\:\/\/)[_.a-z0-9-]+\.[a-z0-9\/_:#=.+?,##%&~-]*[^.|\'|\# |!|\(|?|,| |>|<|;|\)])/', '$0', $text);
}
Expression taken from http://rickyrosario.com/blog/converting-a-url-into-a-link-in-csharp-using-regular-expressions/

Categories