Editing a link with preg_replace or str_replace issue - php

I have link:
http://www.facebook.com/sharer.php?u=http://xxx.xxx.xxx.xxx/~user/file.php
and I want to make it look like this:
http://www.facebook.com/sharer.php?u=http://MyUrl.com/file.php
Essentially I want to replace "xxx.xxx.xxx.xxx/~user" with "MyUrl.com"
Thank you so much for taking the time to help!
One more thing. The file name "file.php" part changes for each page. I don not know ahead what the exact page name will be. Ideally I am wanting to use the php script as an include that will be on every page. I hope this helps clear things up.

where's the problem with this?
http://php.net/manual/fr/function.str-replace.php
$newURL = str_replace("xxx.xxx.xxx.xxx/~user","MyUrl.com","http://www.facebook.com/sharer.php?u=http://xxx.xxx.xxx.xxx/~user/file.php");
UPDATED QUESTION :
you want to swich the xxx.xxx.xxx.xxx/~user AND the file.php
sorry but at this point why don't you just do
http://www.facebook.com/sharer.php?u=http://".$_SERVER['HTTP_HOST']."/".$filename

Related

Stuck trying to redirect urls in PHP

I've searched around and struggled to come up with a solution to this.
I've inherited a project with several thousand php files, each of which has multiple links in the form of:
<a href="_link.php?[RANDOMSTRING]">
Trouble is, I don't have the _link.php file.
I'm assuming its some kind of redirect script, as it is supposed to send the user to
RANDOMSTRING.php
when clicked.
It doesn't do anything nice like use a variable name like
_link.php?url=[RANDOMSTRING]
What code do I need to put into _link.php to just get it working for now. Its a hacky job and I'm planing a major overall and sticking all of this content into a database, but for now I just need the flatfile version running.
Cheers for your help.
Assuming that there are not actual [], then a hack is to create the file _link.php and inside, either redirect:
<?php
header("location: {$_SERVER['QUERY_STRING']}.php");
exit;
Or possibly include if that would work:
<?php
include("{$_SERVER['QUERY_STRING']}.php");
If there are actual [] then just trim them:
trim($_SERVER['QUERY_STRING'], '[]');

PHP eval code and store the result into a variable

I have continued my voyage into creating a extremely simple template engine.
Because I wanted to add logic to my template I eventually got back to the point that I allowed PHP tags into my code which I enabled by evalling the code.
Maybe not the best solution but when looking at the WordPress templates I noticed that the idea itself may not be that bad.
But now there still is one small problem left.
And that is that I want to translate the generated code.
But it has been evalled already. Hence parsed.
I thought of solving this problem by using ob_get_contents().
But this brought one more question and in case of errors it shows a white screen. (memory usage etc.)
Plus it still did not take away the problem of eval that it parsed the contents when evalled.
In short the class logic is:
Loading template files
Adding the contents
Compiling the template
Eval the code (but unfortunately also displaying the code)
Translate the code so I can translate the code parsed by a PHP script
I would love something like:
$code = eval('?>'.$tpl.'<?php');
$code = translate($code);
WriteCache($code);
SetDocumentHeader();
echo $code;
Would anyone know how to achieve this?
Thanks in advance!
$code = eval($tpl);
Check this out.

Changing a link to a redirect page php

How do i change a url in a string example :
This is a link http://google.com.au
To something like this
This a warning page link http://google.com.au
EDIT:
What i'm trying to do is take a description entered by the end user, They might enter links in the description, i want to change all the links to make them GOTO a warning page, the warning page is ./warn.php?site=link
The string might look like this
This is a awesome description Google and this another link Google images
Ok here's what i tried:
$descc = mysql_real_escape_string($_POST['description']);
$descc = preg_replace('"\b(http://\S+)"', '$1', $descc);
Check this, although im not sure if you are really refering to this, just let me know the case then ---
$mylink = "http://google.com.au";
This a warning page link http://google.com.au
EDIT version 1.0
Even it is on description box data you can fetch it via jquery or php like
$mylink = $_GET['desc_name_data'];
Please be more specific with the problem :)
EDIT Version 1.1
Check this and let me know then --
echo preg_replace('(<a href="http://\S+)', '<a href="./warn.php?site='.'google.com.au'.'">google.com.au', $descc);
I'm not sure I understand your question, but urlencode may help.
http://google.com.au
If this isn't it, then please be a bit more specific with what you're trying to achieve and what you have tried.
EDIT:
Ok, you could try a HTML parser to extract the href from the link, then rewrite appropriately. This is likely to be more reliable that a regex.
You should still add urlencode if you're passing a url as a querystring.
You could have a look at preg_replace().

Drupal 6: Render Views 2 data in a page tpl file

I think the answer might be obvious but I can't seem to figure it out.
How do I render a view I've created in Views 2 (Drupal) in a page-pagename.tpl.file? My thought was that it would be some sort of PHP snippet but I can't find documentation about it.
Any thoughts that might steer me in the right direction.
The solution is here:
http://views-help.doc.logrus.com/help/views/embed
Also "function" can be replaced with "print" and the additional param brackets can be removed.
So it will look like this:
$name = "yourviewnamegoeshere";
print views_embed_view($name, $display_id = 'default');

How to pass part of url in new link? Using only HTML & PHP

I have been trying to attempt to use the facebook share function in my website but i cant seems to have the right result.
Say:
i have a page called http://www.example.com/product.php?prod=lpd026n&cat=43
and i am using facebook's share function to have visitors to share the page in the FB wall.
i tried writing the link this way but i doesn't seems to be successful:
href="http://www.facebook.com/share.php?u=www.example.com/proddetail.php?<?php print urlencode(#$_SERVER['QUERY_STRING']!=''?'?'.$_SERVER['QUERY_STRING']:'')?>"
as the result the arguments in the URL came out to be in %26, %3D and etc..
Ie: example.com/proddetail.php?prod%3Dlpd026n%26cat%3D43
as some of you may know that the data after '?' is dynamic and i am planing to use the code above in the frame of the page, so it will have different query passed to the share link in every new item.
The end result that i want got to look like this:
http://www.facebook.com/sharer.php?u=http://www.example.com/proddetail.php?prod=lpd026n&cat=43
Not
http://www.facebook.com/share.php?u=http://www.example.com/proddetail.php?prod%3Dlpd026n%26cat%3D43
can anyone help me to solve this problem?
Thanks in advance!
Ps: if you are unclear, please ask me to further clarify.
This URL:
http://www.facebook.com/share.php?u=http://www.example.com/proddetail.php?prod%3Dlpd026n%26cat%3D43
is only partially-encoded. You actually need to fully URL-encode it before passing to FB, so that it won't interfere with FB's URL structure. I'm sure that their script will know how to parse it properly.
The correct method is:
$url = 'http://www.facebook.com/sharer.php?u='.urlencode('http://www.example.com/proddetail.php?prod=lpd026n&cat=43');
// evaluates to:
// http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.example.com%2Fproddetail.php%3Fprod%3Dlpd026n%26cat%3D43
Update: build your dynamic query
// Original URL
$url = 'http://www.example.com/proddetail.php';
if ($_SERVER['QUERY_STRING'])
$url .= '?'.$_SERVER['QUERY_STRING'];
// Final URL for FB
$fb_url = 'http://www.facebook.com/share.php?u='.urlencode($url);
This is what urlencode does, what is the problem with the link this way?
Edit: I do not use PHP, but I think the following will do the trick (omitted the urlencode):
href="http://www.facebook.com/share.php?u=www.example.com/proddetail.php?<?php print $_SERVER['QUERY_STRING']?>"
I guess K Prime is right.
u need to encode the whole url because the slashes and ":" are still causing problems in this link ;)
$url = 'http://www.facebook.com/sharer.php?u='.urlencode('http://www.example.com/proddetail.php?prod=lpd026n&cat=43');
should be fine for your purposes.

Categories