Link not concatenating the way I imagine it to be - php

I created this variable to store this string:
$extension = '/index.php?Ah83kL80='.$id;
And I'm trying to add the $extension to this link:
<a href="'.Yii::app()->createUrl('image/index',array('album'=>$album->content)).'">
So far, I tried doing this:
<a href="'.Yii::app()->createUrl('image/index',array('album'=>$album->content, 'index' => $extension)).'">
But it put some sort of other characters that I never intended to include.
Output:
/index/%2Findex.php%3FAh83kL80%3D

According to the documentation I found, the second param is an array of URL parameters. That means that you don't want a string like '?Ah83kL80='.$id but want to pass the Ah83kL80 key and $id value separately, as you're already doing with album and $album->content.
I'm not familiar with Yii but try building your link like this... I'm going to format this differently than you have so I can indent and make it easier to read.
$link = Yii::app()->createUrl(
'image/index',
array(
'album'=>$album->content,
'Ah83kL80' => $id,
),
);

Related

Getting the second part of a URL through PHP

I'm trying to setup a page that pulls just a part of the URL but I can't even get it to echo on my page.
Since I code in PHP, I prefer dynamic pages, so my urls usually have "index.php?page=whatever"
I need the "whatever" part only.
Can someone help me. This is what I have so far, but like I said, I can't even get it to echo.
$suburl = substr($_SERVER["HTTP_HOST"],strrpos($_SERVER["REQUEST_URI"],"/")+1);
and to echo it, I have this, of course:
echo "$suburl";
If you need to get the value of the page parameter, simply use the $_GET global variable to access the value.
$page = $_GET['page']; // output => whatever
your url is index.php?page=whatever and you want to get the whatever from it
if the part is after ? ( abc.com/xyz.php?......) , you can use $_GET['name']
for your url index.php?page=whatever
use :
$parameter= $_GET['page']; //( value of $parameter will be whatever )
for your url index.php?page=whatever&no=28
use :
$parameter1= $_GET['page']; //( value of $parameter1 will be whatever )
$parameter2= $_GET['no']; //( value of $parameter2 will be 28 )
please before using the parameters received by $_GET , please sanitize it, or you may find trouble of malicious script /code injection
for url : index.php?page=whatever&no=28
like :
if(preg_match("/^[0-9]*$/", $_GET['no'])) {
$parameter2= $_GET['no'];
}
it will check, if GET parameter no is a digit (contains 0 to 9 numbers only), then only save it in $parameter2 variable.
this is just an example, do your checking and validation as per your requirement.
You can use basic PHP function parse_url for example:
<?php
$url = 'http://site.my/index.php?page=whatever';
$query = parse_url($url, PHP_URL_QUERY);
var_dump(explode('=', $query));
Working code example here: PHPize.online

Stuck with REQUST_URI Parsing ( PHP )

I have a page working as I need it to, with the last /arist-name/ parsing into the correct variable, but the client is adding /artist-name/?google-tracking=1234fad to their links, which is breaking it.
http://www.odonwagnergallery.com/artist/pierre-coupey/ WORKS
http://www.odonwagnergallery.com/artist/pierre-coupey/?mc_cid=b7e918fce5&mc_eid=[UNIQID] DOES NOT WORK
$expl = explode("/",$_SERVER["REQUEST_URI"]);
$ArtistURL = $expl[count($expl)-1];
$ArtistURL = preg_replace('/[^a-z,-.]/', '', $ArtistURL);
Please help, I have been searching for a solution. Thanks so much!
PHP has a function called parse_url which should clean up the request uri for you before you try to use it.
parse_url
Parse a URL and return its components
http://php.net/parse_url
Example:
// This
$url_array = parse_url('/artist/pierre-coupey/?mc_cid=b7e918fce5&mc_eid=[UNIQID]');
print_r($url_array);
// Outputs this
Array
(
[path] => /artist/pierre-coupey/
[query] => mc_cid=b7e918fce5&mc_eid=[UNIQID]
)
Here is a demo: https://eval.in/873699
Then you can use the path piece to perform your existing logic.
If all your URLs are http://DOMAIN/artist/SOMEARTIST/
you could do:
$ArtistURL = preg_replace('/.*\/artist\/(.*)\/.*/','$1',"http://www.odonwagnergallery.com/artist/pierre-coupey/oij");
It would work in this context. Specify other possible scenarios if there are others. But #neuromatter answer is more generic, +1.
if you simply want to remove any and all query parameters, this single line would suffice:
$url=explode("?",$url)[0];
this would turn
http://www.odonwagnergallery.com/artist/pierre-coupey/?mc_cid=b7e918fce5&mc_eid=[UNIQID]&anything_else=whatever
into
http://www.odonwagnergallery.com/artist/pierre-coupey/
but if you want to specifically remove any mc_cid and mc_eid parameters, but otherwise keep the url intact:
$url=explode("?",$url);
if(count($url)===2){
parse_str($url[1],$tmp);
unset($tmp['mc_cid']);
unset($tmp['mc_eid']);
$url=$url[0].(empty($tmp)? '':('?'.http_build_query($tmp)));
}else if(count($url)===1){
$url=$url[0];
}else{
throw new \LogicException('malformed url!');
}
this would turn
http://www.odonwagnergallery.com/artist/pierre-coupey/?mc_cid=b7e918fce5&mc_eid=[UNIQID]&anything_else=whatever
into
http://www.odonwagnergallery.com/artist/pierre-coupey/?anything_else=whatever

Remove certain part of Posted value with php

Hello everyone im trying to retrieve data from a form with a POST request.
This data is posted into another website.
On the website where the data is created i have a text field called website. The data filled in this field goes to the other website where the data is collected. Now i want to exclude the 'www' part. for example if the user enters www.hello.nl i want to receive hello.nl only.
What i tried:
function website () {
$str = $_POST['billing_myfield12'];
echo chop($str,"www");
}
// end remove www
// prepare the sales payload
$sales_payload = array(
'organization_id' => $organization_id,
'contact_id' => $contact_id,
'status' => 'Open',
'subject' => $_product->post_title." ".website(), <----- here i call it
This is not working. Is there a way to do this?
You can use trim() or specifically ltrim() to trim way the www. on the left side. Please don't forget the . after www.
echo ltrim($str, "www.");
Sample Code
echo ltrim("www.hello.nl", "www."); // hello.nl
Demo: http://ideone.com/bqMY7X
Looks like there are side effects with the above code. Let's go with the traditional str_replace method:
echo str_replace("www.", "", $str);
Also, we are sure that it should replace only from the first characters. So, we need to use a preg_replace instead, making it replace from the start.
echo preg_replace("/^www\./g", "", $str);
Verified the above code with: https://regex101.com/r/dv8N6d/1

How to retrieve script name from full absolute URL using PHP?

I'm able to retrieve the full URL like: http://www-click08-co-uk/wonga.php
and I need to retrieve the script name "wonga" from it.
The url will be changing depending on what page the user is on and I will always need the word or phrase after the / and not including the .php, in the example above I would like to create a variable with the value of this being wonga
This is the code I currently have, where "argos" is, is where the database is searched and responds with the information I need, this is where the vaiable would be used
<?php
//-----------------------------------------------------
// Include files and set Classes
//-----------------------------------------------------
require_once $_SERVER["DOCUMENT_ROOT"] . "/includes/common.php";
$db = new dbConnection();
$directorydata = new directorydata();
$phoneDirectory = new phoneDirectory();
$conn = $db->pdoConnect();
// Load the directorydata row via the row ID - 543 is "best buy"
//$directorydata->get($db, 543);
// Load the directorydata row via the url alias field
$directorydata->get($db, "Argos");
// Phone number isn't formatted coming out the DB
$formattedPhoneNumber = $phoneDirectory->formatPhoneNumber($directorydata->Number1);
?>
Just because you didn't provide any code, I provide you a way to solve this on your own:
$url = "http://www-click08-co-uk/wonga.php";
// SEARCH and replace
// FIRST_FUNCTION => google => php trailing name component of path
// SECOND_FUNCTION => google => php explode a string by string
// THIRD_FUNCTION => google => php pop first element of array
$urlParts = SECOND_FUNCTION( ".", FIRST_FUNCTION( $url ) );
echo THIRD_FUNCTION( $urlParts );
OUTPUT:
wonga
An example use of parse_url could be:
$url = 'http://www-click08-co-uk/wonga.php?page=74';
$route = parse_url($url, PHP_URL_PATH);
$routeTokens = explode('/', $route);
$scriptName = array_pop($routeTokens);
echo $scriptName;
which in this case outputs wonga.php.
Just note that this is a very rare task that you would have to take care of yourself. So instead of parse_url you might look at the bigger picture here and start looking for some good MVC framework.

removing unneeded query from generated url string in php

I have the following string generating mp3 urls for a music player on my site.
<?php echo $song->getTitle() ?>
which results in /public/music_song/df/74/746b_2112.mp3?c=ec1e
I would like to remove the query from the string
resulting in /public/music_song/df/74/746b_2112.mp3
I've looked into how to split the url, but I'm nowhere near being a php genius just yet so I dont know weather to split or use preg_replace or how the heck to incorporate it into my existing string.
I have to get rid of these queries, they are unneeded and crashing my databases on a daily basis.
list($keep) = explode('?', '/public/music_song/df/74/746b_2112.mp3?c=ec1e');
$parsedInput = parse_url('/public/music_song/df/74/746b_2112.mp3?c=ec1e');
echo $parsedInput['path'];
// Results in /public/music_song/df/74/746b_2112.mp3
Edit: Since I havent worked with SocialEngine, Im guessing that what you need to do is:
<?php $parsed = parse_url($song->getFilePath());
echo $this->htmlLink($parsed['path'],
$this->string()->truncate($song->getTitle(), 50),
array('class' => 'music_player_tracks_url',
'type' => 'audio',
'rel' => $song->song_id )); ?>

Categories