Edit:
Sorry I wasnt very clear with my first post. Ok I have text stored in the database, Id like to process it and look for any links to the products on my site and turn them into a fancy link. Below is 2 links and my current exp. The second link (Link2) Works as expected and passes the value 21 to detailsLink() My problem lies with Link1 it passes the value of 2 to detailsLink() not 21 as expected. I want to disregard the url completely as detailsLink() recreates the link with the product rating/cat etc. It should also work on http or https links.
Link1: http://192.168.2.22/dev/details.php?id=21
Link2: http://192.168.2.22/dev/details.php?id=21&module=info#Desc
$s = preg_replace("%(?<![=\]\'\"\w])https?://[^<>\s\'\",]*$ADDRESS\/details\.php\?id=([0-9]+)[^<>\s\'\",]+%ie", "detailsLink(\\1)", $s);
the value of $ADDRESS is 192.168.2.22/dev
No need to reinvent the wheel:
$url="http://192.168.2.22/dev/details.php?id=21&module=info#Desc";
parse_str(parse_url($url,PHP_URL_QUERY), $arr);
print_r($arr);
gives you:
Array
(
[id] => 21
[module] => info
)
parse_str()
parse_url()
I don't understand. Why don't you just use $_GET['id']?
EDIT: Never mind. I never knew about those functions! (parse_url, that is)
Related
I want to build an URL in WordPress site with a pattern which looks like this:
mydomain.com/[category-name]/[slug]/[photo-{0-9}]/
Tried re-writing URLs and added "photo" param to query var to catch the photo number, but couldn't succeed.
Am already using a plugin to remove /category/ from URLs.
Any idea to achieve it?
Thanks in advance!
You can use it like this
yourdomain.com/%category%/%postname%/photo-%post_id%/
it is not clear that what are [slug] and {0-9} are. But I think you can manage with them with this answer
Was able to get this working
Adding the solution here so someone can make use or tune it further
I did this adding following rewrite key value pair to $wp_rewrite->rules array:
'([^)]+)/([^)]+)/(photo-\d+)?$' => 'index.php?category_name=$matches[1]&name=$matches[2]&photo=$matches[3]'
This regex groups (inside parenthesis()) url to $matches array which is turn assigned to WP query string.
I want to find the country code of my site visitor using the ipinfodb API.
When I try the following, http://api.ipinfodb.com/v3/ip-country/?key=<My_API_Key>&ip=<Some_IP>, it gives the following output:
OK;;<Some_IP>;US;UNITED STATES
How do I filter this output so that it shows only the country code ?
Regards,
Timothy
EDIT:
In reply to Charles,
After searching on google I came to know that the API can be given a 'format attribute as XML' so the following works.
$xml = simplexml_load_file('http://api.ipinfodb.com/v3/ip-country/?key=<My_API>&format=xml&ip=<Some_IP>);
echo $xml->countryCode;
How can I get the same output without the XML argument ?
Thanks
OK;;<Some_IP>;US;UNITED STATES
How do I filter this output so that it shows only the country code ?
I find it curious that you'd be able to invoke SimpleXML, but didn't think of explode, which will turn a string in into an array, splitting on the given delimiter. In this case, we need to explode on ;:
$string_data = 'OK;;127.0.0.1;US;UNITED STATES';
$sploded_data = explode(';', $string_data);
print_r($sploded_data);
echo "\nCountry code: ", $sploded_data[3], "\n";
This should emit:
Array
(
[0] => OK
[1] =>
[2] => 127.0.0.1
[3] => US
[4] => UNITED STATES
)
Country code: US
You may wish to review the rest of the string manipulation functions to see if there's any other interesting things you may have missed.
The above answer isn't particularly helpful, because the server output is only static if you explicitly specify the same IP address every time. If you hardcode a location request for a particular IP address, it's going to the be the same every time. What's the point?
Do this instead
Install the PHP class for their API. PHP Class
Play around with the PHP sample found here; save it as its own webpage and observe the results. Let's call it "userlocation.php." Please note that the fields will be null if you load from localhost.
Okay, the trick to parse this output is array_values(). Took me forever to figure this, but eventually I stumbled upon this.
So...
$locations = array_values($locations);
echo $locations[n],"<br />\n";
echo $locations[n+1],"<br />\n";
etc.
Whatever element you need, you can get in this way -- and it's dynamic. The code will return whatever country pertains to the user's IP address.
One last note. Take care to paste your API key into the class file and not into userlocation.php. The class file variables are protected, which is a good thing.
Anyway, I'm no expert; just thought I'd share what I've learned.
I'm parsing some HTML, that I have generated in a form. This is a token system. I'm trying to get the information from the Regexp later on, but somehow, it's turning up only the first of the matches. I found a regexp on the Web, that did almost what I needed, except of being able to process multiple occurances.
I want to be able to replace the content found, with content that was generated from the found string.
So, here is my code:
$result = preg_replace_callback("/<\/?\w+((\s+(\w|\w[\w-]*\w)(\s*=\s*(?:\".*?\"|'.*?'|[^'\">\s]+))?)+\s*|\s*)\/?>\[\*.*\*\]\<\/[a]\>/i", array(get_class($this), 'embed_video'), $str);
public function embed_video($matches)
{
print_r($matches);
return $matches[1] . 'foo';
}
I really need only the attributes, since they containt all of the valuable information. The contents of the tag are used only to find the token. This is an example of what needs to happen:
<a type="TypeOfToken1" id="IdOfToken1">[*SomeTokenTitle1*]</a>
<a type="TypeOfToken2" id="IdOfToken2">[*SomeTokenTitle2*]</a>
After the preg_replace_callback() this should be returned:
type="TypeOfToken1" id="IdOfToken1" type="TypeOfToken2" id="IdOfToken2"
But, the callback function outputs the matches, but does not replace them with the return. So, the $result stays the same after the preg_replace_callback. What could be the problem?
An example with real data:
Input:
<p><a id="someToken1" rel="someToken1">[*someToken1*]</a> sdfsdf <a id="someToken2" rel="someToken2">[*someToken2*]</a></p>
returned $result:
id="someToken1" rel="someToken1"foo
Return from the print_r() if the callback function:
Array ( [0] => [*someToken1*] sdfsdf [*someToken2*] [1] => id="someToken1" rel="someToken1" [2] => rel="someToken1" [3] => rel [4] => ="someToken1" )
I think that it is not returning both of the strings it should.
For anyone else stumbling into a problem like this, try checking your regexp and it's modifiers.
Regarding the parsing of the document, I'm still doing it, just not HTML tags. I have instead gone with someting more textlike, that can be more easily parsed. In my case: [*TokeName::TokenDetails*].
I have a file that contains a bunch of links:
site 1
site 2
site 3
I want to get the URL to a link with specific text. For example, search for "site 2" and get back "http://site2.com"
I tried this:
preg_match("/.*?[Hh][Rr][Ee][Ff]=\"(.*?)\">site 2<\/[Aa]>.*/", $contents, $match)
(I know the HREF= will be the last part of the anchor)
But it returns
http://site1.com">site 1</a><a href="http://site2.com
Is there a way to do a search backwards, or something? I know I can do preg_match_all and loop over everything, but I'm trying to avoid that.
Try this:
preg_match("(<a.*?href=[\"']([^\"']+)[\"'][^>]?>site 2</a>)i",$contents,$match);
$result = $match[1];
Hope this helps!
Or you can try using phpQuery.
I'm trying to use brackets within the name of a cookie.
It is supposed to look like this(this is how the browser wants it!):
Name: name[123456].newName
Content: 20
Here is my example:
$cookie = "name[123456].newName=20"
But when I analyze what the browser sees, I get this:
cookie['name'] = Array
And I want:
cookie['name[123456].newName'] = 20
My question is: How should I write the cookies name in a way that the browser understands?
Thank you in advance.
Actually, all you have to do is this:
<?php
setcookie('name[123456].newName', 20);
?>
This generates the following header:
Set-Cookie: name[123456].newName=20
... and browsers (well, at least Firefox) seem to handle it just fine.
The issue starts when you want to read the value back. PHP has an otherwise nice feature: whenever it finds an input parameter (get, post, cookie...) with square brackets on its name, it'll build an array from it. So print_r($_COOKIE) displays this:
Array
(
[name] => Array
(
[123456] => 20
)
)
I'm not aware of any way to disable this feature so you probably need to use string functions and parse the contents of the raw cookie, which can be found at $_SERVER['HTTP_COOKIE']:
name[123456].newName=20