Jump to content

Defining a URL from a TAG


johnrb87

Recommended Posts

Hi all

 

I wonder if someone can help me, basically I have a HTML form with a free text area, which looks like

 

<textarea name="details"></textarea>

 

when I submit the form, I can print what was outputted using

 

$details = $_POST['details'];
print $details;

 

The user is able to either enter plain text, like

 

this is my website profile

 

or they can enter some text and a URL, like

 

this is my website [http://www.google.com,2] profile

 

My question is, when I do my print statement using

 

print $details;

 

how can I get it to convert what has been posted, so that it outputs the following HTML

 

this is <a href="http://www.google.com">my website</a> profile

 

so basically it would create a link using what was entered between

 

[]

 

and then use

 

,2

 

to determine how many words before the URL should be linked, so in my example, just the words

 

my website

 

would be linked, as I defined

 

,2

 

following the URL in the code.

 

But then further to this, if I entered 0, using

 

,0

 

how can I then get it to link all the text, like

 

<a href="http://www.google.com">this is my website profile</a>

 

i'm very new to PHP, so some guidance would be a great help

 

Thanks very very much, been racking my brain on how to do this and have tried so many things, all with massive failure

Link to comment
https://forums.phpfreaks.com/topic/209850-defining-a-url-from-a-tag/
Share on other sites

try

<?php
/* 
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
$test = 'this is my website [http://www.google.com,2] profile';
$test = preg_split('/[\[\]]/', $test);
$first = explode(' ', trim($test[0]));
$url = split(',', $test[1]);
$in = '';
if(count($first) < $url[1]) $url[1] = count ($first);
for($i =0; $i < $url[1]; $i++){
    $in = array_pop($first).' '. $in;
}
$out = implode(' ', $first). ' <a href="'. trim($url[0]) . '">' . trim($in) .'</a>'.$test[2];
print_r($out);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.