/    Sign up×
Community /Pin to ProfileBookmark

Problem reading URLs from file

I’m making a basic news section, with a script to add news and another to read it, which will be embedded in an HTML page eventually. I’m fairly new to PHP, and I’ve got an issue with URLs in the content.

The URL is added to the file ok, the file looks like this –

[code]
12.07.05|Simon|Let’s try a hyperlink test <a href=”http://www.msn.com”>for MSN</a>
[/code]

and the PHP code that reads and displays the file is this –

[code]
<?php
$data = file(‘news.txt’);
$data = array_reverse($data);
foreach($data as $element) {
$element = trim($element);
$element = preg_replace(‘/(http|ftp)+(s)?:(//)((w|.)+)(/)?(S+)?/i’, ‘<a href=””>4</a>’, $element);

$pieces = explode(“|”, $element);
echo $pieces[2] . “<BR>” . “<b>Posted by ” . $pieces[1] . ” on ” . $pieces[0] . “</b><BR><BR>”;
}
?>
[/code]

When I run it the link isn’t displayed correctly. When I vew source of the HTML page that is displayed it has this –

[code]
Let’s try a hyperlink test <a href=”<a href=”http://www.msn.com”>for”>www.msn.com</a> MSN</a><BR><b>Posted by Simon on 12.07.05</b><BR><BR>
[/code]

As you can see, the URL of the home site is being added, plus the actual URL isn’t being formatted correctly.

Can anyone help with this????

If you provide a solution can you try and take the time to explain bascially what is going wrong, so I can understand PHP a bit better? I’ve only been using it for a few days.

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@bokehDec 09.2005 — That is because [B][/B] contains the global match that the regex finds.

Post the target string and the result you are expecting!
Copy linkTweet thisAlerts:
@avaDec 09.2005 — Try this (easier) function to display your links:

[code=php]$data = file('news.txt');
$data = array_reverse($data);
foreach($data as $element)
{
$elementarray = explode('|',$element);
$lines = $elementarray[2] . "<BR>" . "<b>Posted by " . $elementarray[1] . " on " . $elementarray[0] . "</b><BR><BR>";
echo $lines;
}[/code]
Copy linkTweet thisAlerts:
@thecornflakeauthorDec 09.2005 — Thanks,

That's working now. I was using the preg_replace function as I found it on a site and it said it read HTML from files correctly, so I asusmed that was what was missing.
Copy linkTweet thisAlerts:
@chazzyDec 09.2005 — Thanks,

That's working now. I was using the preg_replace function as I found it on a site and it said it read HTML from files correctly, so I asusmed that was what was missing.[/QUOTE]


If you're saving the HTML to a file (why not use a database?) anything should be able to read it.
Copy linkTweet thisAlerts:
@thecornflakeauthorDec 12.2005 — I'm not using a database because my current hosting account doesn't allow it - I'm in the process of transferring to one that does....
×

Success!

Help @thecornflake spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...