can anyone help me to piece together the puzzled I'm facing. Lets say I have url's
/some-work/
/store/bread/alloy/
and in both of these cases I wanna fetch the first part from it. i.e. some-work, store.
Now I've used parse_url(get_permalink()) to get the array of the url and then fetch the path index of the array to fetch the above string. Now I have also checked strstr PHP function, but I am unable to make it work. Can anyone help?
You can use explode, array_filter and current function like as
$url = "http://www.example.com/some-work/";
$extracted = array_filter(explode("/",parse_url($url,PHP_URL_PATH)));
echo current($extracted);//some-work
Demo
Related
I have these variables:
$summary="The problem with field2 is field13. The fix will be field7"
$_POST['field2']='thiscomputer';
$_POST['field7']='thishotfix';
$_POST['field13']='thisapplication';
I'm trying to craft a preg_replace() that will find /field[0-9]/ within the string and replace it with the value from the $_POST array. But I keep coming up short. Maybe preg_replace() is the wrong function to use in this instance.
I'm trying to replace an old long list of 50+ str_replace's
Thanks for any help that can point me in the right direction
How about this single line str_replace instead of preg_replace? You may need to fix some spacing, that can be resolved by the array value
echo str_replace(array_keys($_POST),array_values($_POST),$summary);
Output:
The problem with thiscomputer is thisapplication. The fix will be thishotfix
WORKING DEMO: https://3v4l.org/quYVO
strtr, though I'd probably want to copy and filter that post array:
<?php
$summary="The problem with field2 is field13. The fix will be field7";
$_POST['field2']='thiscomputer';
$_POST['field7']='thishotfix';
$_POST['field13']='thisapplication';
echo strtr($summary, $_POST);
Output:
The problem with thiscomputer is thisapplication. The fix will be thishotfix
In a PHP application, $_SERVER['HTTP_REFERER'] has the following value:
http://testing.localhost/userdashboard/test/fc
I have try this $value= striurl($_SERVER['HTTP_REFERER'], 'test');, the value I get is test/fc.
My question is what is the proper way to extract the value of "fc"?
Thanks a lot for any help.
Laravel's Request class has a function called segments() which returns an array of all segments in the url.
here it would be = to ['userdashboard', 'test', 'fc']
So with that in mind, you can grab the last piece with...
$lastSegment = last(request()->segments());
try this
echo end(explode("/",$_SERVER['HTTP_REFERER']));
The function you're looking for is basename().
$base = basename('test/fc');
echo $base; // fc
You might also want to look at parse_url(), which will extract all the elements of a URL into a nice array structure.
I'm working on a project where all of the members and their info are stored in a JSON file. I'm in the process of creating a search form and I need help on how to iterate through the members and check to see if there's an exact match or a similar match.
The members are stored in a SESSION variable:
$_SESSION['members'] = json_decode($jsonFile);
but I'm uncertain how to use regex to check for matches that are similar (and not just exact). For example, if a member's name is "Jonathan", I'd like that result to be returned even if the user searches "Jon". Is regex the correct approach? Any help will be greatly appreciated - thank you!
-Manoj
I think I'd be using a database to store the data rather than JSON so that you can use the LIKE searches, e.g.
SELECT * FROM users WHERE name LIKE 'Jon%'
If you absolutely have to use JSON you could loop through all members and use a regexp like
preg_match('/^'.$term.'.*/i', $element, $matches);
to check them all.
If the $jsonFile contents is an array of some sort, you may find preg_grep() of use, though it doesn't work on multidimensional arrays. You might have have to loop over each individual member record and grep the relevant fields yourself, something like:
foreach ($_SESSION['members'] as $idx => $member) {
... match relevant fields...
}
http://localhost:8888/test.php
I typed in the above URL to navigate to a blank php file I'd created, and my friend typed the following in my URL bar:
?one=3&two=18
(after /test.php)
He said that when I refresh the page, he wants to see the number 21 show up. I'm not entirely sure where to start. Any help for this PHP beginner is appreciated.
Thanks!
Try this...
<?php
if (isset($_GET['one']) AND isset($_GET['two'])) {
$one = (int) $_GET['one'];
$two = (int) $_GET['two'];
echo $one + $two;
}
? is the operator used to indicate the start of the parameters passed to a webpage. You can pass many of them separated by the character &. Based on that you need your website to get those parameters and perform the only operation that will get you the result 21 from 18 and 3.
So you need your webpage to display the sum of your first parameter and your second parameter.
You can get those by using $_GET.
Bottom line what you need is:
Learn how to get the parameters passed
Learn how to do operations with them (sum)
Learn how to display the result.
Learn about UrlEncode
Good luck on the php world!
to obtain something from your query string (?... part of the URL) you have to use $_GET[] i.e.
$one = $_GET['one'];
$two = $_GET['two'];
echo $one+$two; //print it
You need to GET the variables from that URL and manipulate them.
http://www.w3schools.com/php/php_get.asp
But you first and foremost need to learn from the ground up. I suggest this tutorial:
http://www.tizag.com/phpT/
<?php
echo $_GET['one'] + $_GET['two'];
?>
print_r($_GET) will show you the anwser.
I am using Janrains PHP openID library. All is working perfectly except when I try and pull values from the query string on the script that is returned to after the user is authorized.
Here is the first part of the query string:
http://localhost/Cloudshare/trunk/Cloudshare/php/openID/recieve_auth_request.php? janrain_nonce=2010-11-08T22:29:59Zp9PTgs&openid.ns=http://specs.openid.net/auth/2.0&openid.mode=id_res&openid.op_endpoint=https://www.google.com/accounts/o8/ud.
I am able to get the value of the 'janrain_nonce' item on the query string, but am not able to get any values for things like 'openid.op_endpoint'..
For example, this
$test = $_GET["janrain_nonce"];
returns a value ok into $test, but the the value of $test is blank using the following...
$test = $_GET["openid.claimed_id"];
What I am trying to do is get one of the values from the query string to see if it contains 'google' or not, as I have some processing specific to Google to do.
Any help is much appreciated.
Thanks.
I didn't see openid.claimed_id in your querystring, but try getting $_GET['openid_claimed_id'];
testing your querystring at my API seems to indicate that the "." is being converted to "_" FWIW
I've confirmed that. var.var2 gets converted to var_var2 see
http://api.fatherstorm.com/?query&test_is[]=1&test.is[]=2