preg_replace change img and link paths to use proxy - php

I've run into a hard problem to deal with. I am replacing a-tags and img-tags to fit my suggestions like this. So far so good.
$search = array('|(<a\s*[^>]*href=[\'"]?)|', '|(<img\s*[^>]*src=[\'"]?)|');
$replace = array('\1proxy2.php?url=', '\1'.$url.'/');
$new_content = preg_replace($search, $replace, $content);
Now my problem is that there are links on pages that i fetch the content of that looks like this:
<a href="/test/page/">
and
<a href="http://google.se/test/">
And when after replacing these two links looks like this:
<a href="proxy2.php?url=/test/page/">
and
<a href="proxy2.php?url=http://google.se/test/">
The problem is for me is that i want to include a variable named $url before /test/page/ and only on that links that are like that, not those who was already http:// or https:// before.

This should do the job for the anchor tags, at least:
<?php
function prepend_proxy($matches) {
$url = 'http://example.prefix';
$prepend = $matches[2] ? $matches[2] : $url;
$prepend = 'proxy2.php?url='. $prepend;
return $matches[1] . $prepend . $matches[3];
}
$new_content = preg_replace_callback(
'|(href=[\'"]?)(https?://)?([^\'"\s]+[\'"]?)|i',
'prepend_proxy',
$content
);
?>

Simply make your proxy2.php a little smarter. If a fully qualified URL comes in (http://...), redirect to that. If a local URL comes in (e.g. /test/page/), drop in what's missing (e.g. http://www.mylittleapp.com/test/page/) and redirect.

This would do the trick
$search = array('#(<a\s*[^>]*href=[\'"]?)(https?://)?#');
$replace = array('\1proxy2.php?url=');
$new_content = preg_replace($search, $replace, $content);
Result:
<a href="proxy2.php?url=/test/page/">
<a href="proxy2.php?url=google.se/test/">

it's me Sara. Scronide, your code did'nt work. It still returns:
<a href="proxy2.php?url=/test/page/">
<a href="proxy2.php?url=google.se/test/">
Instead of what i wanted it to show, i wanted it to show like this, with the url prepended:
<a href="proxy2.php?url=**THEURLHERE.COM**/test/page/">
<a href="proxy2.php?url=google.se/test/">
SORRY, IT DID WORK, I WAS DOING SOMETHING WRONG WITH THE URL VARIABEL. THANK U SCRONIDE!

Related

How to make substr_replace match the whole string

<?php
$titledb = array('经济管理','管理','others');
$content='经济管理是我们国的家的中心领导力,这是中文测度。';
$replace='<a target="_blank" href="http://www.a.com/$1">$1</a>';
foreach ($titledb as $title) {
$regex = "~\b(" . preg_quote($title) . ")\b~u";
$content = preg_replace($regex, $replace, $content, 1);
}
echo $content;
?>
I was writing a auto link function for my wordpress site and I'm using substr_replace to find the keywords(which are litterally a lot) and replace it with link--I'm doing this by filtering the post content of course.
But in some circumstances, suppose there are posts with titles like "stackoverflow" and "overflow" it turns out to be a mess, the output will look like :
we love<a target="_blank" href="http://www.a.com/stackoverflow">stackoverflow</a>,this is a test。we love <a target="_blank" href="http://www.a.com/stack<a target=" _blank"="">overflow</a> ">stackoverflow,this is a test。
What I want is:
we love<a target="_blank" href="http://www.a.com/stackoverflow">stackoverflow</a>,this is a test。we love stack<a target="_blank" href="http://www.a.com/overflow">overflow</a>,this is a test。
And this is only a test.The production enviorment could be more complicated,like I said there are tens of thousands of titles as keywords need to be found and replaced with a link. So I see these broken links a lot.It happens when a title contains another title.Like title 'stackoverflow' contains another title 'overflow'.
So my question is how to make substr_replace take title 'stackoverflow' as a whole and replace only once? Of course,'overflow' still needs to be replaced somewhere else just not when it is included in another keyword.
Thank you in advance.
To prevent that a search for a word will start replacing inside the HTML code that you already injected for some other word, you could make use of a temporary placeholder, and do the final replacement on those place holders:
$titledb = array('经济管理','管理','others');
// sort the array from longer strings to smaller strings, to ensure that
// a replacement of a longer string gets precedence:
usort($titledb, function ($a,$b){ return strlen($b)-strlen($a); });
$content='经济管理是我们国的家的中心领导力。';
foreach ($titledb as $index => $title) {
$pos = strpos($content, $title);
if ($pos !== false) {
// temporarily insert a place holder in the format '#number#':
$content = substr_replace($content, "#$index#", $pos, strlen($title));
}
}
// Now replace the place holders with the final hyperlink HTML code
$content = preg_replace_callback("~#(\d+)#~u", function ($match) use ($titledb) {
return "<a target='_blank' href='http://www.a.com/{$titledb[$match[1]]}'>{$titledb[$match[1]]}</a>";
}, $content);
echo $content;
See it run on eval.in

PHP string replace for single URL

I've been reading various articles and have arrived at some code.
For a single URL on my site http://home.com/example/ (and only that URL - no children) I would like to replace all instances of "<a itemprop="url" with just <a basically stripping out itemprop="url" This is what I have come up with but I'm not sure whether I'm on the right lines and if I am how to 'echo' it on on the basis it's code and not something to be echoed to screen. Also not too sure whether I need to escape the double quotes within the single quotes in $str_replace.
if(preg_match("%/example/$%", $_SERVER['REQUEST_URI'])){
$string = "<a itemprop=\"url\"";
$str_replace = str_replace('<a itemprop="url"','<a',$string);
//something here
}
Please could anyone advise also if I am correct in how I am approaching this what the final part of the code needs to be to run it (I'm assuming not echo $str_replace;. I'll be running it as a function from my Wordpress functions.php file - I'm comfortable with that if it works.
This could be a mess and I apologise if it is.
try strpos()
if(strpos($_SERVER['REQUEST_URI'], "example") !== false){
$string = "<a itemprop=\"url\"";
$str_replace = str_replace('<a itemprop="url"','<a',$string);
}
There must be some kind of template where you get the default html and modify it with the php at some point of your code...
$html_template = file('...adress_of_the_url_template...');
.......
if(strpos($_SERVER['REQUEST_URI'], "example") !== false){
$string = "<a itemprop=\"url\"";
$html_template = str_replace($string,'<a',$html_template);
}
.......
.......
echo $html_template
Then you have replaced the html code as you wanted
It looks like I was over-complicating it because the solution appears to be within Wordpress functions. This is what I've ended up with. Any comments, corrections or recommendations appreciated. I'm not a coder as you may realise...
function schema( $content ) {
if (is_page( 'my-page-slug')) {
return str_replace('<a itemprop="url"', '<a', $content);
}
else return $content;
}
add_filter('the_content', 'schema', 99);

Using str_replace to format a link into an image with embeded hyperlink

I'm not sure if I'm going about this the correct way. I would like to take a simple link, like this;
https://www.youtube.com/watch?v=examplevideo
and turn it into
<a href= 'https://www.youtube.com/embed/examplevideo' target=_blank><img src='http://img.youtube.com/vi/examplevideo/0.jpg' width='536' border='1'></a>
In the past, I've been able to change links by using a str_replace, which was very straightforward since you would pull out one pattern and just replace it with another. But, in this case, the pattern that's being kept shows up twice in the output. Is a str_replace the right way to go about it?
Here's one simple way to do it...
// $video_url = "https://www.youtube.com/watch?v=examplevideo";
$videoId = str_replace("https://www.youtube.com/watch?v=", "", $video_url);
enter code here
$videoLink = "<a href= 'https://www.youtube.com/embed/$videoId' target=_blank><img src='http://img.youtube.com/vi/$videoId/0.jpg' width='536' border='1'></a>"
Of course, if your URL is more complex (e.g. ?v=abc&t=123) then this won't work, and you would have to parse the URL more like a URL (i.e. not using str_replace).
You can use parse_url() and parse_str() to get the video ID, and then use sprintf() to build the embed code.
I've made a small function:
function getEmbedded($url) {
$parts = parse_url($url);
$parsed = parse_str($parts['query'], $params);
$result = sprintf("<a href= 'https://www.youtube.com/embed/%s'
target=_blank><img src='http://img.youtube.com/vi/%s/0.jpg'
width='536' border='1'></a>", $params['v'],$params['v']);
return $result;
}
Usage:
echo getEmbedded($url);
This is more efficient than using str_replace() and works even when there are additional query parameters in the video URL.

Change a relative URL to absolute URL

for example i've got a string like this:
$html = '
test
test
test
hi
';
and i want to append the absolute url to all hrefs where no abolute domain is given.
$html = '
test
test
test
hi
';
whats the best way to do that? i guess something with RegEx, but my RegEx skills are ** ;)
thanks in advance!
found a good way :
$html = preg_replace("#(<\s*a\s+[^>]*href\s*=\s*[\"'])(?!http)([^\"'>]+)([\"'>]+)#", '$1http://mydomain.com/$2$3', $html);
you can use (?!http|mailto) if you have also mailto links in your $html
$domain = 'http://mydomain';
preg_match_all('/href\="(.*?)"/im', $html, $matches);
foreach($matches[1] as $n=>$link) {
if(substr($link, 0, 4) != 'http')
$html = str_replace($matches[1][$n], $domain . $matches[1][$n], $html);
}
The previous answer will cause problems with your first and fourth example because it fails to include a forward slash to separate the page from the page name. Admittedly this can be fixed by simply appending it to the $domain, but if you do that then href="/something.php" will end up with two.
Just to give an alternative Regex solution you could go with something like this...
$pattern = '#'#(?<=href=")(.+?)(?=")#'';
$output = preg_replace_callback($pattern, 'make_absolute', $input);
function make_absolute($link) {
$domain = 'http://domain.com';
if(strpos($link[1], 'http')!==0) {
if(strpos($link[1], '/')!==0) {
return $domain.'/'.$link[1];
} else {
return $domain.$link[1];
}
}
return $link[1];
}
However it is worth noting that with a link such as href="example.html" the link is relative to the current directory neither method shown so far will work correctly for relative links that aren't in the root directory. In order to provide a solution that is though more information would be required about where the information came from.

Using the php substr on the_permalink() in WordPress

My urls for posts in WordPress looks like this:
http://localhost:8888/blabla/book/yes-vi-testar
Using the_permalink() would generate "http://localhost:8888/blabla/book/yes-vi-testar" but I want to cut the first 34 characters to get a string like "yes-vi-testar". How do I use php substr in a case like this? I'm confused... I tried
<?php
$friendlypermalink = substr(the_permalink(), 34);
?>
but that doesnt do the trick.
Use get_the_permalink to get the permalink without echoing it
So
substr(get_the_permalink(), .............);
A lot of of the Wordpress function have 'return' alternates using get as the operative word. IE: get_the_time, get_the_content, etc.
the_title is the only one I believe that doesn't have this option. For the_title you have to pass two empty parameters (the before and after seperators) and either a true or false ... not sure at the moment
the_title("","",true);
As Chacha says, use get_the_permalink(). You can then do something like:
$url = get_the_permalink();
$text = substr($url, strrpos($url, '/') + 1);
//or
preg_match('~[^/]+$~', get_the_permalink(), $m);
$text = $m[0];

Categories