473,386 Members | 1,943 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,386 software developers and data experts.

string http:// conwert to <a href=''>host</a>

hi

in form TEXTAREA i have text like:

bla bla http://www.google.com/bla/bla/bla text text text text
now how to make from this text like:

bla bla ['http://www.google.com'] text text text text
['']-> url to http://www.google.com/bla/bla/bla
I wrote this script:
Were error is?
ps. sorry , my english is not good.:)
<?
function string_url($text) {

$arr=explode("\n", $text);
$j=0;
foreach( $arr as $val)
{

if( strlen($val) > 1 )
{
$arr2=explode(" ", $val);
$i=0;
foreach( $arr2 as $val2)
{

$findme = 'http';
$pos = strpos($val2, $findme);
if ($pos === false)
{

$arr2[$i] = $val2;
}

else {

$val2=parse_url($val2);
$host=$val2['host'];
$path=$val2['path'];
$scheme=$val2['scheme'];
$tekst="<a HREF=\"$scheme://";
$tekst.=$host;
$tekst.=$path;
$tekst.=">$host</a>";
$arr2[$i]=$tekst;
}

$i++;
}//koniec 2 forech (slowa)
$slowa=implode(" ", $arr2);
$arr[$j] = $slowa;

}
else
{
$arr[$j] = $val;
}

$j++;
}//koniec 1 forech
$text_nowy= implode("\n", $arr);
return $text_nowy;
}


if ( $tresc) {

echo string_url($tresc);
} else {
print "<FORM METHOD=POST > </B><B>title:</B><BR>";
print "<TEXTAREA NAME=tresc ROWS=30 COLS=60
$tresc</TEXTAREA><BR>";

print "<br><INPUT TYPE=\"submit\" VALUE=\"add!\"><br><br>";
print "</FORM>";
}
?>

Oct 14 '05 #1
5 2069
Am Fri, 14 Oct 2005 12:23:24 -0700 schrieb specjal:
hi

in form TEXTAREA i have text like:

bla bla http://www.google.com/bla/bla/bla text text text text
now how to make from this text like:

bla bla ['http://www.google.com'] text text text text
['']-> url to http://www.google.com/bla/bla/bla
I wrote this script:
Were error is?
ps. sorry , my english is not good.:)
<?
function string_url($text) {

$arr=explode("\n", $text);
$j=0;
foreach( $arr as $val)
{

if( strlen($val) > 1 )
{
$arr2=explode(" ", $val);
$i=0;
foreach( $arr2 as $val2)
{

$findme = 'http';
$pos = strpos($val2, $findme);
if ($pos === false)
{

$arr2[$i] = $val2;
}

else {

$val2=parse_url($val2);
$host=$val2['host'];
$path=$val2['path'];
$scheme=$val2['scheme'];
$tekst="<a HREF=\"$scheme://";
$tekst.=$host;
$tekst.=$path;
$tekst.=">$host</a>";
$arr2[$i]=$tekst;
}

$i++;
}//koniec 2 forech (slowa)
$slowa=implode(" ", $arr2);
$arr[$j] = $slowa;

}
else
{
$arr[$j] = $val;
}

$j++;
}//koniec 1 forech
$text_nowy= implode("\n", $arr);
return $text_nowy;
}


if ( $tresc) {

echo string_url($tresc);
} else {
print "<FORM METHOD=POST > </B><B>title:</B><BR>";
print "<TEXTAREA NAME=tresc ROWS=30 COLS=60
$tresc</TEXTAREA><BR>";

print "<br><INPUT TYPE=\"submit\" VALUE=\"add!\"><br><br>";
print "</FORM>";
}
?>


$tresc = preg_replace('/(http)(s?)(://)([^ ]+)/', '<a
href="$1$2$3$4">$1$2$3$4</a>', $tresc);

--
-------------------------------------------------------
Try this: SCA the Smart Class Archive for PHP
http://www.project-sca.org
-------------------------------------------------------

Oct 14 '05 #2
Warning: Unknown modifier '/'
what is wrong?

$tresc = preg_replace('/(http)(s?)(://)([^ ]+)/', '<a
href="$1$2$3$4">$1$2$3$4</a>', tresc);

Oct 15 '05 #3
specjal wrote:
Warning: Unknown modifier '/'
what is wrong?

$tresc = preg_replace('/(http)(s?)(://)([^ ]+)/', '<a
href="$1$2$3$4">$1$2$3$4</a>', tresc);


The slashes are used as modifiers to mark the beginning and the end of the
pattern, but they are also used in the pattern itself.

You can fix this by escaping the slashes in the pattern:

$tresc = preg_replace('/(http)(s?)(:\/\/)([^ ]+)/', '<a
href="$1$2$3$4">$1$2$3$4</a>', tresc);

Or by using another modifier:

$tresc = preg_replace('#(http)(s?)(://)([^ ]+)#', '<a
href="$1$2$3$4">$1$2$3$4</a>', tresc);
JW

Oct 15 '05 #4
Janwillem Borleffs wrote:
specjal wrote:
Warning: Unknown modifier '/'

$tresc = preg_replace('/(http)(s?)(://)([^ ]+)/', '<a
href="$1$2$3$4">$1$2$3$4</a>', tresc);


The slashes are used as modifiers to mark the beginning and the end of the
pattern, but they are also used in the pattern itself.


No -- the slashes are used as *delimiters*. The *modifiers* are any extra
characters that occur after the end delimiter. For example:

/foo/i

will match 'foo' case-insensitively. In this example, the 'i' is a
modifier -- it modifies the behaviour of the regular expression to make it
case-insensitive.

Specjal's warning is caused by the third slash. PHP sees the second slash
as the end delimiter, which makes any trailing characters modifiers.

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact

Oct 15 '05 #5
Toby Inkster wrote:
No -- the slashes are used as *delimiters*. The *modifiers* are any
extra characters that occur after the end delimiter.


Yeah, you're right.
JW

Oct 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Nick Keighley | last post by:
I'm probably missing something rather obvious, but is there a known problem with getting a Python based socket program to communicate with a C based socket program? A simple echo server written in...
0
by: Matt | last post by:
I'm trying to get the HTML data off of a webpage. Let's say for the sake of argument it's the python homepage. I've googled around and found some examples that people said worked. Here's what...
0
by: news.microsoft.com | last post by:
hello, all. I am trying to convert a struct from VB to c# in order to use an old DLL. Here is the definition in VB Public Type CHost Host As String * 60 Port As String * 5 Info As...
0
by: juannorton | last post by:
Hi got db2 v8.2 on win 2003 I configurated the smtp server so i can receive notifications from the task center, but my smtp server refuse the email adddress from the sender. If any way to...
8
by: Mark | last post by:
We have a multi-line textbox that users copy and paste email text into. The pasted text frequently will contain a tag like <blah@aol.com> or similar. I believe .NET is protecting itself from code...
10
by: Dia | last post by:
At the company I work job applicants are required to do a little test. The human resource manager recently had a candidate who claimed one of the questions was ambiguous. Dependent upon the...
5
by: KL | last post by:
I have a problem trying to write to a binary file that I want to name after a particular name from a set. My code for the function follows, please excuse the horrible mistakes you may see, I am a...
8
by: Benjamin Bécar | last post by:
Hello everyone. I have to find a correct architecture to achieve this XML <=Text conversion platform. The platform (based on Win2003Server) will have to deal with 21 million XML files and 16...
2
dlite922
by: dlite922 | last post by:
Hello Hello! I'm trying to connect to a host that accepts UDP on a port I setup and tested from a command line too called udp_write. However a PHP fsocketopen() call doesn't work nor does it give...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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?
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.