Jump to content

rikbignell

New Members
  • Content count

    2
  • Joined

  • Last visited

Community Reputation

0 Neutral

About rikbignell

  • Rank
    Newbie
  1. rikbignell

    fgetcsv with regex replacement for an url

    Thank you so much for taking the time to help me on this. I love forums like this where coders are just there to help each other. I wish my email notifications were working though as i only just saw this. I spent another 2 hours trying to get it to work and managed to fudge it. I like your way much better though so i'll use your code. Here's my sloppy attempt. Feels good that i worked through it and learned a few things about php. <?php // Create a table from a csv file echo "<html><body><table>\n\n"; $f = fopen("speedtest.csv", "r"); while (($line = fgetcsv($f)) !== false) { $row = $line[0]; // We need to get the actual row (it is the first element in a 1-element array) $cells = explode(";",$row); $pattern = '/(^http.*png$)/'; $replacement = '<a href=$1>Link</a>'; echo "<tr>"; foreach ($cells as $cell) { echo "<td>" . preg_replace($pattern,$replacement,$cell) . "</td>"; } echo "</tr>\n"; } fclose($f); echo "\n</table></body></html>"; ?>
  2. Hi, I have cobbled together some code to display output from a speedtest csv as a php page. The output shows the data exactly how i want but leaves the url as text. I would like the url replaced with href to make it a link. I have included my attempts but this is not working. I feel like im close but ${1} is not putting the link into the href. Before: http://www.richardbignell.co.uk/speedtest.php After: http://www.richardbignell.co.uk/speedtest1.php As you can see it shows the link as i want but the link contains nothing. What am i doing wrong? I'm not looking for a better way of doing this, i'd like to just fix the preg_replace command please. <?php // Create a table from a csv file echo "<html><body><table>\n\n"; $f = fopen("speedtest.csv", "r"); while (($line = fgetcsv($f)) !== false) { $row = $line[0]; // We need to get the actual row (it is the first element in a 1-element array) $cells = explode(";",$row); echo "<tr>"; foreach ($cells as $cell) { echo "<td>" . preg_replace("/http.*png/","<a href=${1}>Link</a>",$cell) . "</td>"; #echo "<td>" . str_replace("http",'<a href="http://www.richardbignell.co.uk">Link</a>',$cell) . "</td>"; #echo "<td>" . htmlspecialchars($cell) . "</td>"; } echo "</tr>\n"; } fclose($f); echo "\n</table></body></html>"; ?>
×

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.