This is my current code:
$parcels = $api->parcels->get();
$url = (array_values($parcels)[0]['label']['label_printer']);
$goToUrl = $api->getUrl($url);
str_replace('/api/v2//api/v2/', '/api/v2/', $goToUrl);
print_r($goToUrl);
echo "<br />";
echo $url;
Why do I use str_replace()? because I am intending to redirect to $goToUrl and this is not working because the current API is giving me the link wrong.
This is my output:
https://api_key:api_secret#panel.sendcloud.nl/api/v2//api/v2/labels/label_printer/1369315
As you can see, api/v2 comes in this link twice. I want to remove /api/v2/ and then run the output. But my str_replace(); is not performing. My output stays the same.
Can this even be achieved this way? Thanks for any help in advance.
Try changing the str_replace line to:
$goToUrl = str_replace('/api/v2//api/v2/', '/api/v2/', $goToUrl);
Related
I need to be able to store and echo regular expressions. In my case the user enters the regex into a form and that exact sequence of characters needs to be echo-ed to the screen sometime later. The problem is that the echo changes the characters.
So for instance I have tried this
$regex = '(?<=amount\">\$)(.*?)(?=</strong>)';
but when I echo it..
echo $regex;
I get...
((((amount\">\$)(.*?)(?=)
If I do this
$regex = htmlentities($regex);
I get this which helped with the missing part of the regex but not the multiple ((((
((((amount\">\$)(.*?)(?=</strong>
htmlspecialchars did not help either.
How do I get it to echo the variable exactly as it is written? And what would I need to do to store them in MySQL and retrieve them exactly as written?
EDIT - in response to some observations below, I add a bit more detail. This new example was done on a PHP 7.1 server in the cloud, Centos 7 rendered using Chrome.
$regex = '(?<=amount\">\$)(.*?)(?=</strong>)';
$page_elements_regex[1][0] = $regex;
$page_elements_regex[1][1] = addslashes($regex);
$page_elements_regex[1][2] = htmlspecialchars($regex);
$page_elements_regex[1][3] = htmlentities($regex);
echo "regex " . $page_elements_regex[1][0] . "<BR>";
echo "addslashes " . $page_elements_regex[1][1] . "<BR>";
echo "htmlspecialcharacters " . $page_elements_regex[1][2] . "<BR>";
echo "htmlentities " . $page_elements_regex[1][3] . "<BR>";
Results
regex ((((amount\">\$)(.*?)(?=)
addslashes ((((amount\\\">\\$)(.*?)(?=)
htmlspecialcharacters ((((amount\">\$)(.*?)(?=</strong>)
htmlentities ((((amount\">\$)(.*?)(?=</strong>)
It is also a big clue that if you take off the first ( like this
$regex = '?<=amount\">\$)(.*?)(?=</strong>)';
The result removes the first a of amount!! Is it interpreting the regex instead of echoing it?
?(((mount\">\$)(.*?)(?=)
I have solved it, and I feel a bit foolish about the answer. My bad.
Somewhere else in my code I had this
$regex[1] = '(?<=amount\">\$)(.*?)(?=</strong>)';
$regex[2] = '(?<=amount\">\$)(.*?)(?=</strong>)';
$regex[3] = '(?<=amount\">\$)(.*?)(?=</strong>)';
I have no idea why this gave the result it did, rather than a straight up error, but once removed it all is fine. The bottom line is that both htmlspecialcharacters and htmlentities give the right answer, Lesson learnt. Check all the code, my mistake was in the use of arrays, defining $regex as an array and a variable, not as I first thought here.
So, I have dynamically generated pages that follows the following format:
http://example.com/name/first_name/last_name/John%2C+Smith
What is the php code to output the last part of the url?
so, it becomes like "John, Smith".
Thank you so much.
EDIT:
I realized that the URL is ended with another / and the answers given below does not pick it up. What change should I make?
http://example.com/name/first_name/last_name/John%2C+Smith/
EDIT 2:
So, the link is dynamically generated as the following:
href="http://example.com/name/first_name/last_name/<?php echo $full_name ?>"
Split the url, get the last fragment then URL decode it:
<?
$urlarray=explode("/",$url);
$end=$urlarray[count($urlarray)-1];
$end=urldecode($end);
//go on using $end then
?>
You could do this with a regex.
echo preg_replace_callback('~.*/(.*)~',
function($matches) {
return urldecode($matches[1]);
},
'http://example.com/name/first_name/last_name/John%2C+Smith');
Regex demo: https://regex101.com/r/bE3bO5/1
Output:
John, Smith
Update:
echo preg_replace_callback('~.*/(.+)~',
function($matches) {
return rtrim(urldecode($matches[1]), '/');
},
'http://example.com/name/first_name/last_name/John%2C+Smith/');
You can use parse_url with second parameter PHP_URL_PATH
$url = urldecode("http://example.com/name/first_name/last_name/John%2C+Smith");
$arr = array_filter(explode('/',parse_url($url, PHP_URL_PATH)));
print_r(end($arr));
Edited:
As per requirement for dynamic url you can use
$url = urldecode("http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]");
i have this URL:
www.blublub.de/niedersachsen.html?veranstaltung=Neumarkt+Osnabrück~W011Cb5ETzN2EJoG9N~10.04.2014
And i like to get the id-number **"W011Cb5ETzN2EJoG9N"** from the URL.
How can i implement this in php ?
please help me.
$parts=explode('~', $_GET['veranstaltung']);
$id=$parts[1];
try this
<?php
$veranstaltung=$_GET['veranstaltung'];
$str=explode('~',$veranstaltung);
$idnumber=$str[1];
echo $idnumber;
?>
get and explode it
try to get and explode with (~) your url query string
$url = $_GET['veranstaltung'];
$myid = explode('~', $url);
echo $myid[1]; // output - W011Cb5ETzN2EJoG9N
Use regular exp for this
if (preg_match_all("/~(.*?)~/",$_GET['veranstaltung'],$result){
print_R($result)
}
$m = 'this_is_m';
$this_is_m = 'this_is_m_not_full :(';
$this_is_m_full = 'this_is_m_FULL!! :D';
print ${$m};
(Hi first :P) Outputs:
this_is_m_not full :(
Any idea how to output this_is_m_FULL!! :D using $m??
What I've already tried:
print $"{$m}_full";
print $'{$m}_full';
print $'$m_full';
print ${$m}_full';
None worked... Thanks in advance,,
The solution would be:
print ${$m . '_full'};
But that feels very hackish.
To get your desired output, you need to do the following:
print ${$m . "_full"};
The following should work:
print ${$m.'_full'};
This is because the string inside the braces will get evaluated first, becoming
print ${'this_is_m' . '_full'}
-> print ${'this_is_m_full'}
-> print $this_is_m_full
Take a look at this manual page if you want more information on this.
Having a major brain freeze, I have the following chunk of code:
// Get web address
$domQuery = query_HtmlDocument($html, '//a[#class="productLink"]');
foreach($domQuery as $rtn) {
$web = $rtn->getAttribute('href');
}
Which obviously gets the entire href attribute, however I only want 1 specific attribute within the href. I.e. If the href is: /website/product1234.do?code=1234&version=1.3&somethingelse=blaah
I only want to return the variable for "version", so wish to only return "1.3" in my example. What's most efficient way to do this?
You could use parse_url and parse_str to extract that information.
Bingo! Thanks webdestroya, parse_str is exactly what I am after:
$string="/website/product1234.do?code=1234&version=1.3&somethingelse=blaah";
parse_str($string,$return);
$version = $return['version'];
echo "Version: " . $version;
Prints:
Version: 1.3