Link to home
Start Free TrialLog in
Avatar of magento
magento

asked on

Easy script

Hi ,

I have a few lines of text in a flat file which i need to convert something like below.

Input
Bari Durrës Bari
Brindisi Vlore

Output
<a title="Bari Durrës Bari" href="http://www.test.fr/bari-durres-bari/">Bari Durrës Bari</a>
<a title="Brindisi Vlore" href="http://www.test.fr/Brindisi-Vlore/">Brindisi Vlore</a>


Thanks,
Magento
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Here you go:

$lines = file('textfile.txt');
foreach ($lines as $line) {
    echo "<a title=\"$line\" href=\"http://www.test.fr/$line\">$line</a><br />";
}

Open in new window

Avatar of magento
magento

ASKER

Hi ,

In the second part of the $line it should add - instead of space.

Thanks
Ooops, sorry:

$lines = file('textfile.txt');
foreach ($lines as $line) {
    $urlline = str_replace(' ', '-', $line);
    echo "<a title=\"$line\" href=\"http://www.test.fr/$urlline\">$line</a><br />";
}

Open in new window

Why is bari-durres-bari lowecased but Brindisi-Vlore capitalized?
Avatar of magento

ASKER

Ozo ,

Thanks for checking in .

Yes, it should be in lowercase only  as my 1st sample in the output .

Sorry for pasting it wrong.

Thanks
SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
perl -Mutf8 -Mcharnames=:full -lpe '$s=$_;utf8::decode$_;s/\s/-/g;s{([^\0-\177])}{charnames::string_vianame(((charnames::viacode ord $1)=~/(.*?)( WITH )?/)[0])}ge;$_=qq{<a title="$s" href="http://www.test.fr/\L$_\E/">$_</a>}'
Avatar of magento

ASKER

Thanks MarqusG, it works well.

Ozo , You are genius in perl :)

I always really wonder what actually is the code .

But as a learning part , can you provide a simple perl script instead one liner with comments?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of magento

ASKER

Thanks everyone for your kind help.

I will close this question sooner.