This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Linkify PHP text
Hi I have a field from a database that contains http links as well as normal text.
I need to find out a way to display this information using PHP to show Text and a link when applicable.
the following is my sql query:
$sql =
"select bw_supporting_doc
from cm3rm2 with(nolock)
where number = '$id'";
the output of this sql statement is:
Please find below the link to the FSS: http://blahblack.html
I need to display the http://blahblack.html as a link at the moment it is displaying as normal text.
Thanks!
function hyperlink($text){
$text = preg_replace("#(http://)+?([.]?[a-zA-Z0-9_/-])*#", "\\0", $text);
return $text;
}
this might do the job
example usage:
$str = 'this is a random string which contains a link http://www.yahoo.com';
echo hyperlink($str);
Related
This question already has answers here:
What's the best way of scraping data from a website? [closed]
(2 answers)
Closed 4 years ago.
I would like to scrape the information of the product with PHP like description, title, price, quantity etc with a use of the url of the product in the amazon website or with the use on the ISBN.
How should I do it? Is there any example of code for me?
Thanks!
With PHP you can get the job done by opening the url with a file_get_contents() and by using a preg_match() with regex.
Then, taking a random article page and browsing its html code, for example https://www.amazon.com/Learning-PHP-MySQL-JavaScript-jQuery/dp/1491978910/
By inspecting the code we can see the price is in a specific <span>:
<span class="a-size-medium a-color-price header-price">
$35.00
</span>
Then we can do something like:
$link = 'https://www.amazon.com/Learning-PHP-MySQL-JavaScript-jQuery/dp/1491978910/';
$page_content = file_get_contents($link);
if(preg_match('/<span class=\"a-size-medium a-color-price header-price\">(.*?)<\/span>/i',
$page_content, $matches)) {
$price = trim($matches[1]);
} else {
echo "Price not found.";
$price = 0;
}
Then you have here enough basis to repeat the operation to get all the other informations you need to fetch.
Notice that it's not the best relevent way to do this as it assumes the HTML patterns are always the same from an article to another. The best relevant way is to use their API if possible.
This question already has answers here:
How do I run PHP code when a user clicks on a link?
(10 answers)
Closed 9 years ago.
Here is the code:
$vote_up = 'Vote Up';
I am simply trying to have my PHP function insert a link that has a line of PHP as the href. But it does not work right. How can I do this?
Quick update: I got some good answers, however I'd like for the function to fire when clicked (hence the need for the <?php tags).
You're trying to use PHP tags inside the string context. They have no meaning and hence won't produce the output you need.
Simply use string concatenation:
$vote_up = 'Vote Up';
Or use sprintf() (bit more cleaner):
$vote_up = sprintf('Vote Up', amc_comment_vote("up"));
this is also a possibility:
$vote_up = "<a href='".amc_comment_vote("up")."'>Vote Up</a>";
This question already has an answer here:
PHP: Properly convert addresses to clickeable links in string
(1 answer)
Closed 9 years ago.
$string1 = "You can view the full calendar at
http://www.something.org/calendar/57682117
or email: info#something.org"
$string2 = "You can view the full calendar at
www.something.org/calendar/57682117
or email: info#something.org"
This is the string(s) I have. What I need to do is convert the two urls to links that will link to a page, and then the emails will need to be converted to mailto: links.
I don't need to account for websites that are blahblah.com. They will always have one of these www. or http(s). I am terrible with regular expressions... so help will be appreciated!
$output = preg_replace('_<a href="(?!mailto:)(?!tel:)[^"]+[^>]*>(.+?)</a>_', '$1', $string1);
echo '<pre>'.print_r(htmlentities($output), true).'</pre>';
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP: STR replace by link
i'm trying to make a website that has a chatbox or a shoutbox..
when i enter a link on the textarea, for example http://link.com/ and submit it, it appears just a plain text on the chatbox, how can i make it like facebook chat any other chat that when you enter a link it will display as a link with the underline that you can click.. and i don't know how to do it..
RegEx and preg_replace are a good start for this:
$pattern = "/http:\/\/(www\.)?([^.]+\.[^.\s]+\.?[^.\s]*)/i";
$replace = "<a href='http://\\1\\2'>http://\\1\\2</a>";
$string = preg_replace($pattern,$replace,$string);
echo $string;
The good point here is, you can type in something like:
Hello, look at my site http://www.google.de
Which will be translated into:
Hello, look at my site http://www.google.de
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Detecting a url using preg_match? without in the string
I wanting to search a string that is input by the user, for instances of http:// if one is found I want to wrap the link in the correct html,
so for example, the following could be input by a user,
Google's url is http://www.google.com,
what I want to save that as it,
Google's url is http://www.google.com
is this possible?
function make_clickable($sText) {
$sClickText = str_replace(' www.', ' http://www.', $sText);
$sClickText = preg_replace("/([\s])?(http|ftp|https)([\:\/\/])([^\s]+)/i", " $2$3$4",$sClickText);
return $sClickText;
}
Tadaa!