473,383 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

How to convert URLs in string into Links and Edit the URL while displaying?

I don't have good knowledge in PHP. I need help with:

Let's suppose a string:

"Why is sky blue? http:www.whyistheskyblue.com/whyisitsoblue/cansomebodymake_it_red"

Ok now for this string, I want to conver the URL mentioned in to link and at a same time I want the URL (since is quite long) be displayed as:

"Why is sky blue? http:www.whyistheskyblue.com/whyisitsob......"

How will that be possible using PHP. Please help me with short and easy PHP script to do so. Thanks...
Jun 18 '08 #1
10 21236
hsriat
1,654 Expert 1GB
I could not understand what you wish to do. Can you please elaborate it?
Jun 18 '08 #2
I could not understand what you wish to do. Can you please elaborate it?
[PHP]

$string="The world is bright. Check this URL: http://bytes.com/forum/newreply.php?do=newreply&p=3182222";
[/PHP]

Now I want to convert URL in $string into link. But replacement must be done in this way:

[HTML]
The world is bright. Check this URL: <a href="http://bytes.com/forum/newreply.php?do=newreply&p=3182222">http://bytes.com/forum/newreply.php?......</a>
[/HTML]

How can I do this using PHP?
Jun 18 '08 #3
r035198x
13,262 8TB
First you need to split the string into the text and url parts.
You can then output an <a> tag using the url. The ... part is even easier since you just need to check if the url exceeds your predefined maximum allowable length and susbtring the url at maximum allowable length - 3 (the -3 is for the dots).
Jun 18 '08 #4
hsriat
1,654 Expert 1GB
or use &hellip; instead of three dots.
Jun 18 '08 #5
I want it to be done using something like ereg_replace. Spliting the string will not be easy when there many urls in string. So I want they be replaced easily please help.
Jun 18 '08 #6
hsriat
1,654 Expert 1GB
See if the User Contributed Notes in this page are of any use.
Jun 18 '08 #7
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $text = "Why is sky blue? http://www.whyistheskyblue.com/whyisitsoblue/cansomebodymake_it_red. hello world!";
  3.  
  4. $new = preg_replace("/(http:\/\/[^\s]+)/", "<a href=\"$1\">$1</a>", $test);
  5.  
  6. print $new; /* will display $text with link to http://www.whyistheskyblue.com/... */
  7. ?>
  8.  
Jun 20 '08 #8
Did you find a solution to this? I got the very same problem right now.

Regards,
Magnus
Jun 26 '08 #9
This should do the trick:

[PHP]
$string = "Hello http://www.bytes.com world";

preg_match('/(http:\/\/[^\s]+)/', $string, $text);
$hypertext = "<a href=\"". $text[0] . "\">" . $text[0] . "</a>";
$newString = preg_replace('/(http:\/\/[^\s]+)/', $hypertext, $string);

echo $newString; //will output Hello <a href="http://www.bytes.com">http://www.bytes.com</a> world

[/PHP]
Oct 23 '08 #10
thank davidc02
Aug 16 '10 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Convert TextBox.Text to Int32 Problem | last post by:
Need a little help here. I saw some related posts, so here goes... I have some textboxes which are designed for the user to enter a integer value. In "old school C" we just used the atoi function...
4
by: Ken Varn | last post by:
I have an unknown numeric Type object passed into a function. I want to run a conversion on a string to convert the string to that Type object and return an object of that type. Is there some way...
15
by: Yifan | last post by:
Hi Does anybody know how to convert System::String* to char*? I searched the System::String class members and did not find any. Thanks Yifan
2
by: David Thielen | last post by:
Hi; Is there a method that will convert a string to the correct text for a url? For example www.dave.com/my name.htm -> www.dave.com/my%20name.htm but & should not be changed to &amp; -- thanks...
2
by: Marcelo Muzilli | last post by:
Howdy all, in my program, I have a ushort variable and a string variable and if I try to compile it, I'm receiving this error message: "Cannot convert type 'string' to 'ushort'". How can I...
2
by: Tio | last post by:
my code: if (new data ) { CString* pStr = (CString*)wParam; //data come with some com port (PostMessage) CString p2Str; //old string in edit box m_dataa.GetWindowText(p2Str);
3
by: priyanka | last post by:
Hi there, I want to convert a String into integer. I get the string from a file using : string argNum; getline(inputStream,argNum); I now need to convert argNum into integer.
1
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does 1+1 equal 11? or How do I convert a string to a number?...
2
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does 1+1 equal 11? or How do I convert a string to a number?...
12
by: aatif | last post by:
I want to convert a string of hex characters (2 hex chars = 1 byte), to ASCII. Hex chars include zeros (0x00) as well, which I want to include in ASCII string. hex string: 5000005355.... ASCII:...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.