0

I want to use a special text in textarea as variable that has value.

For example:

A text %Url% in textarea is equal to http://example.com/.

It is more likely to this:

If In PHP, it will work like this:

$Url = 'http://example.com';
echo 'This is an example'.$Url.'of a website';

output in html: This is an example http://example.com of a website

What I want to do is make the textarea work like the code above.

Textarea

%Url% = http://example.com/

<textarea name='test'>This is an example %Url% of a website</textarea>

fiddle

So when the value of that textarea is sent to the server, I want php to read it as:

This is an example http://example.com/ of a website

or if $_POST['test'] (the textarea name is test) is echoed after submission, then it will output:

This is an example http://example.com/ of a website

How to achieve it? I saw an app able to do it!

Ari
  • 4,643
  • 5
  • 36
  • 52
  • 2
    bog standard "PHP 101" code you could have whipped up yourself: `This is an example of a website` – Marc B Dec 12 '13 at 14:29

2 Answers2

2

I would approach this using str_replace

echo "<textarea name='test'>" . str_replace("%Url%", "http://example.com", $url) . "</textarea>";
MrHunter
  • 1,892
  • 1
  • 15
  • 23
0

If your posting the URL to your PHP then you can just collect it as normal (as Marc B said). But if you're displaying it back to screen then remember to escape it to prevent cross site scripting (XXS) attacks. Use htmlentities() to escape any incoming HTML.

So something as simple as this should do it:

$url = htmlentities($_POST['Url']); 
echo "This is an example {$url} of a website";
Community
  • 1
  • 1
Adam
  • 1,098
  • 1
  • 8
  • 17