Forum Moderators: coopster
I've been trying to do this for a while and have not been able to successfully do it or find any good info on Google. I have a script that submits a form, and all the variables that aren't $_POST variables will be cleared.
Is there any way to preserve an ordinary variable? The solution I have is pretty dirty but it works, I just save it as a text file and open it in the code executed after the form has been submitted, to avoid conficts using tmpfile() would work except it generates a different string before and after the form is submitted, so I use the md5 algorithm and multiply the user agent, ip and host. Clearly this isn't the way to code.
I've got another little problem, with regular expressions.
I want to parse URLs from within some text, I want to hyperlink regular [example.com...] and also http://www.example.com [example.com]. I have regular expressions that work for either, but they conflict when I use both of them.
I use this for a regular URL:
$text = preg_replace("~(http://\S+¦www\.\S+)~i", "<a href=\"\\1\">\\1</a>",$text);
And this for UBB URLs:
$text = preg_replace("/\[url=(.*?)\](.*?)\[\/url\]/i","<a href=\"\\1\">\\2</a>",$text);
I believe they could work in harmony if the top regex didn't parse URLs that have an equals sign preceding them, eg. =http://... because all [url] formatted URLs have this.
I found it surprisingly difficult to find a regex for the [url] formatted link that actually worked, I had to take a few in the end and build it with them.
Thanks.
[edited by: jatar_k at 7:02 pm (utc) on Jan. 4, 2005]
[edit reason] examplified [/edit]
You're replacing first [......] with <a
and the www... with <a, so you are replacing the same url twice (i don't think that's what you want).
Eg. $text = "http://www.bla.com is a nice page"
after first replace you get:
<a href="http://www.bla.com">http://www.bla.com</a> is a nice page
after second replace you get:
<a href="http://<a href="www.bla.com">www.bla.com</a>">http://<a href="www.bla.com">www.bla.com</a></a> is a nice page
That's completely horrendous!
To achieve goal leave replace [..,...] but the second you should replace urls that must be without [....]
I don't know how to do that, but it's possible.
With preserving variables it's a bit tricky. You can use session to preserve them, or database, or if the data is not very relevant place it into <input name="variables" type="hidden" value="<?echo $preserve_variables;?>" />
As for the regex problem, try adding an assertion, like:
$text = preg_replace("~(?<!=)(http:// ... ;
...to your regular url pattern. That should exclude cases where http is preceeded by an =
I hope this helps.
$text = preg_replace('/(?<!=)(?<!])(http¦ftp)+(s)?:(\/\/)((\w¦\.)+)(\/)?(\S+)?/i','<a href="\0">\0</a>',$text );
$text = preg_replace('/\[url=(.*?)\](.*?)\[\/url\]/i','<a href="\\1">\\2</a>',$text);
The first expression stripped the "http://" from the visible output, but changing <a href="\0">\4</a> to <a href="\0">\0</a> fixed that.
I had success using the hidden fields in forms, but for private information, I've left using files. What about session variables?