PHP - Add link to a URL in a string
function processString($s) {
return preg_replace('/https?:\/\/[\w\-\.!~#?&=+\*\'"(),\/]+/','<a href="$0">$0</a>',$s);
}
PHP - Add link to a URL in a string if not present
Well, I just modified your lookbehind path to (?<!href="|">)
to avoid the link inside the tags...
So use :
function processString($s){
return preg_replace('@(?<!href="|">)(https?:\/\/[\w\-\.!~?&=+\*\'(),\/]+)((?!\<\/\a\>).)*@i','<a href="$1">$1</a>',$s);
}
Regex101 Demo
Codepad Demo
How do I linkify urls in a string with php?
You can use the following:
$string = "Look on http://www.google.com";
$string = preg_replace(
"~[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]~",
"<a href=\"\\0\">\\0</a>",
$string);
PHP versions < 5.3 (ereg_replace) otherwise (preg_replace)
How to put a hyperlink inside a text in php
You use seemingly Wordpress so this is not a php function but a Wordpress function _e ();
Here is a link and you can see how to do it with Wordpress:
https://wordpress.stackexchange.com/questions/114201/html-inside-or-e-language-translation-string
But if you want to do that only with php, then an echo with your link in the text as well as in HTML will suffice:
<?php echo 'This is a link <a href="https://developer.wordpress.org/reference/functions/_e/">click here</a> for more.';
But you can also write a function yourself that works like Wordpress:
https://developer.wordpress.org/reference/functions/_e/
Use the simple form or break out the markup
<?php echo __( 'Please read <a href="https://goo.gl">this</a>', 'example-domain' );
or, but all not good by translation
<?php _e( 'This is a link <a href="https://wordpress.stackexchange.com/questions/165357/how-to-make-a-text-with-hyperlink-translatable-in-wordpress">click here</a> for more', 'example-domain' );
break out the html, try this one and you can more shorten this but for understand
$anchor = esc_html_x( 'Google', 'link text for google.com', 'txt-domain' );
$domain = esc_url( __( 'google.com', 'txt-domain' ) );
$link = sprintf( '<a href="https://%s">%s</a>', $domain, $anchor );
/* translators: 1 is a link with text "Google" and URL google.com */
echo sprintf( esc_html__( 'Use %1$s to search.', 'example-domain' ), $link );
The web is full of stuff:
https://wordpress.stackexchange.com/questions/165357/how-to-make-a-text-with-hyperlink-translatable-in-wordpress
How to add hyperlink to a word which is a part of a string in PHP?
You have your string in single quotes. When you single quote your string, you are telling PHP to display the string as-is. Thus, the $link
variable will not be interpreted.
An alternative to MuthaFury's solution is to change your single quotes to double quotes to tell php that you do want the $link
variable interpreted.
echo "Hi all welcome to the google page <a href=\"$link\">Google Page</a>";
Modify links in a string with PHP
I think a more comfortable solution could be using str_replace (http://php.net/manual/en/function.str-replace.php)
do something like this:
$string = str_replace(
['http://','https://'],
['http://redirect.com?link=http://', 'http://redirect.com?link=https://'],
$sourceString
);
PHP Regex - How to append to a URL (in a string variable with a lot of text) where there is an a href
You should use a parser, not a regex for this.
$html = 'This is my <a href="http://www.google.com">Website</a>.';
$dom = new DOMDocument();
$dom->loadHTML($html);
$links = $dom->getElementsByTagName('a');
foreach($links as $link) {
$link->setAttribute('href', $link->getAttribute('href') . '?utm=foo_foo_foo');
}
echo $dom->saveHTML();
Output:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><p>This is my <a href="http://www.google.com?utm=foo_foo_foo">Website</a>.</p></body></html>
If you had to use a regex you could do
$html = 'This is my <a href="http://www.google.com">Website</a>.';
echo preg_replace('~href=("|\')(.+?)\1~', 'href=$1$2?utm=foo_foo_foo$1', $html);
Output:
This is my <a href="http://www.google.com?utm=foo_foo_foo">Website</a>.
Both these approaches presume you never have a ?
in the URL already..
Related Topics
Pass a Percent (%) Sign in a Url and Get Exact Value of It Using PHP
Difference Between Mt_Rand() and Rand()
In PHP, How to Check If a Function Exists
Using PHP to Download Files, Not Working on Large Files
3Rd Party Dependency Conflict in Developing Wordpress Plugin
How to Select Default Value of a Field
File_Get_HTML Displays Fatal Error Call to Undefined Function
Pdo: Invalid Parameter Number: Mixed Named and Positional Parameters
Php: $_Get and $_Post in Functions
Get Variables from the Outside, Inside a Function in PHP
Laravel - Model Class Not Found
How to Use String Concatenation to Define a Class Const in PHP
How to Make a Cascading Drop Down List in PHP Using Jquery
How to Obtain a Nested HTML List from Object's Array Recordset
Concatenate Values of N Arrays in PHP
Laravel Livewire Component Not Refreshing/Reloading Automatically After Refreshing It