473,223 Members | 1,728 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,223 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 2062
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: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.