How work with preg_replace to replace with excluded pattern - php

I have some text contain html tags, I would like to replace all links with other one, but I want to replace just local links, not they start with http://
example :
test link
==> test link
Video
==> Video
I try this preg_replace but not working :
$exclude = '<a href=\"http://.*?';
$pattern = '<a href=\".*?';
$content=preg_replace("~(($exclude)?($pattern))~i",'<a href="/action.php?url=$4',$content);
Thanks!

What about something like this:
$content = preg_replace('#<a href="([^:]*)">#i', '<a href="/action.php?url=$1">', $content);

Related

Woocommerce remove link in description product with preg_replace

Following a prestashop site transfer to woocommerce, I would like to remove all the old links added in the product description tab in order to avoid 404 errors. The description.php file contains :
the_content();
I would like to remove all the links present in the_content(), but keep the text.
I've tried preg_replace like this :
$content = the_content();
echo preg_replace("/<a\s+href=['\"]([^'\"]+)['\"][^\>]*>[^<]+<\/a>/i",'$1', $content);
but it does not work.
Does this do what you want? I move the parentheses in the regex.
$string = "Remove the link (<a href='http://example.com'>Link Text 1</a>) here";
$string .= "\n";
$string .= 'Remove another (Link Text 2) link';
$pattern = "/<a\s+href=['\"][^'\"]+['\"][^\>]*>([^<]+)<\/a>/i";
$replacement = '';
echo preg_replace($pattern, '$1', $string);
Result
Remove the link (Link Text 1) here
Remove another (Link Text 2) link

Replacing Relative Links with External Links in PHP String

I am working with an editor that works purely with internal relative links for files which is great for 99% of what I use it for.
However, I am also using it to insert links to files within an email body and relative links don't cut the mustard.
Instead of modifying the editor, I would like to search the string from the editor and replace the relative links with external links as shown below
Replace
files/something.pdf
With
https://www.someurl.com/files/something.pdf
I have come up with the following but I am wondering if there is a better / more efficient way to do it with PHP
<?php
$string = 'A link, some other text, A different link';
preg_match_all('/<a[^>]+href=([\'"])(?<href>.+?)\1[^>]*>/i', $string, $result);
if (!empty($result)) {
// Found a link.
$baseUrl = 'https://www.someurl.com';
$newUrls = array();
$newString = '';
foreach($result['href'] as $url) {
$newUrls[] = $baseUrl . '/' . $url;
}
$newString = str_replace($result['href'], $newUrls, $string);
echo $newString;
}
?>
Many thanks
Lee
You can simply use preg_replace to replace all the occurrences of files starting URLs inside double quotes:
$string = 'A link, some other text, A different link';
$string = preg_replace('/"(files.*?)"/', '"https://www.someurl.com/$1"', $string);
The result would be:
A link, some other text, A different link
You really should use DOMdocument for such job, but if you want to use a regex, this one does the job:
$string = '<a some_attribute href="files/something.pdf" class="abc">A link</a>, some other text, <a class="def" href="files/somethingelse.pdf" attr="xyz">A different link</a>';
$baseUrl = 'https://www.someurl.com';
$newString = preg_replace('/(<a[^>]+href=([\'"]))(.+?)\2/i', "$1$baseUrl/$3$2", $string);
echo $newString,"\n";
Output:
<a some_attribute href="https://www.someurl.comfiles/something.pdf" class="abc">A link</a>, some other text, <a class="def" href="https://www.someurl.com/files/somethingelse.pdf" attr="xyz">A different link</a>

Only match the href="http inside the reges

I'm using the following regex to select the href="http part inside an url which doesn't contain a rel="nofollow" yet:
preg_replace(
"/<a\b(?=[^>]+\b(href=\"http))(?![^>]+\brel=\"nofollow\")/',
"rel=\"nofollow\" href=\"http://",
$input_string
);
The thing is it only replaces the <a because that's the first match.
How is it possible to select the a tag but exclude the <a part from the results so it only will match href="http? Because preg_match does return <a AND href="http, but I only need href="http :)
The reason I think this might be the only right solution is because it's not sure how many <a> tag the given string contains and whether they contain a rel=nofollow or not. I need to make sure I only replace the http:// with rel="nofollow" http:// inside <a> tags with no rel="nofollow"
EDIT 1:
giuseppe straziota asked for an input and output example so here it is:
input:
this is a string with a lot of content and links and whatever....
output:
this is a string with a lot of content and <a rel="nofollow" href="http://information.nl" class="aClass">links</a> and whatever....
EDIT 2:
I run a couple of more tests, these are the results:
code (exact copy/paste):
$input_string = 'this is a string with a lot of content and links and whatever....';
$input_string = preg_replace(
'/<a\b(?=[^>]+\b(href="http))(?![^>]+\brel="nofollow")/',
'rel="nofollow" href="http://',
$input_string
);
echo htmlentities($input_string);
result from php 7.0.5:
this is a string with a lot of content and rel="nofollow" href="http:// href="http://information.nl" class="aClass">links</a> and whatever....
And it should be:
this is a string with a lot of content and <a rel="nofollow" href="http://information.nl" class="aClass">links</a> and whatever....
EDIT 3:
I tried this regex:
$test = preg_replace(
'/(?=<a\b[^>]+\b(href="http))(?![^>]+\brel="nofollow")/',
'rel="nofollow" href="http://',
$input_string
);
But now it places the 'rel="nofollow" href="http://', right before the <a, so the result:
rel="nofollow" href="http://links
Not exactly what I want either...
I'm thinking too difficult, I made some adaptions in my preg_replace so I can just use the first regex:
$test = preg_replace(
'/<a(?=\b[^>]+\b(href="http))(?![^>]+\brel="nofollow")/',
'<a rel="nofollow"',
$input_string
);
It replaces the <a tag, so I should have taken advantage of that like I do now.

concate string with href path in php

I have a file with so many href attributes. I want to modify the path, just want to add absolute path with the existing.
e.g href="parentall_files/filelist.xml" , just needs to be changed to href="dir/parentall/parentall_files/filelist.xml" throughout the file.
I have written the following:
$contents = preg_replace('/<a href="(.*?)"/i', '<a href="dir\/parentall\/$"',$contents);
But alas! it is not changing the path.
Please help.
Why you don't just change it with str_replace()
$contents = str_replace('href="parentall_files/','href="dir/parentall/parentall_files/', $contents);
Try
preg_replace('/<a href="(.*?)"/', '<a href="dir\/parentall\/"',$contents)
You need to add the {1} after $ sign
$string = 'asdsadsad';
$new = preg_replace('/<a href="(.*?)"/i', '<a href="dir/parentall/${1}', $string);
Output is:
string '<a href="dir/parentall/parentall_files/filelist.xml>asdsadsad</a>' (length=65)
you can use str_replace.
$contents= str_replace('href="','href="dir/parentall/',$contents);

replace any url's within a string of text, to clickable links with php

Say i have a string of text such as
$text = "Hello world, be sure to visit http://whatever.com today";
how can i (probably using regex) insert the anchor tags for the link (showing the link itself as the link text) ?
You can use regexp to do this:
$html_links = preg_replace('"\b(https?://\S+)"', '$1', $text);
I write this function.
It replaces all the links in a string. Links can be in the following formats :
www.example.com
http://example.com
https://example.com
example.fr
The second argument is the target for the link ('_blank', '_top'... can be set to false).
Hope it helps...
public static function makeLinks($str, $target='_blank')
{
if ($target)
{
$target = ' target="'.$target.'"';
}
else
{
$target = '';
}
// find and replace link
$str = preg_replace('#((https?://)?([-\w]+\.[-\w\.]+)+\w(:\d+)?(/([-\w/_\.~]*(\?\S+)?)?)*)#', '<a href="$1" '.$target.'>$1</a>', $str);
// add "http://" if not set
$str = preg_replace('/<a\s[^>]*href\s*=\s*"((?!https?:\/\/)[^"]*)"[^>]*>/i', '<a href="http://$1" '.$target.'>', $str);
return $str;
}
Edit: Added tilde to make urls work better https://regexr.com/5m16v

Categories