Hi PHPers, Suppose I have got a text file which contains text like Welcome some text http://example.com/sdsada/ some text Some more words Some text http://example.com/sdsada/asdsads some text .. .. .. Code (markup): I want to read this file in php and convert it automatically to something like Welcome some text <a href="http://example.com/sdsada/mytext">http://example.com/sdsada/</a> some text Some more words Some text <a href="http://example.com/sdsada/asdsads/mytext">http://example.com/sdsada/asdsads/</a> some text .. .. .. Code (markup): Your help is much appreciated.
That is easy: <?php $filename = 'test.txt'; $outputfile = 'newfile.txt'; $data = file_get_contents($filename); $data = preg_replace('@(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@','<a href="$1" target="_blank">$1</a>', $data); file_put_contents($outputfile, $data); PHP:
possible non-regex solution: 1. explode it 2. run each word through a FILTER (URL or something) 3. if it is a URL simply append a tag
will the 'some text' before the href always be the same? if so you could do string replace ..to replace the 'some text' and white space with the new formatting. simple fix