Solved

Easy script

Posted on 2013-12-13
11
282 Views
Last Modified: 2014-01-05
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
0
Comment
Question by:magento
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39718294
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

0
 
LVL 5

Author Comment

by:magento
ID: 39718301
Hi ,

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

Thanks
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39718304
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

0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 84

Expert Comment

by:ozo
ID: 39718310
Why is bari-durres-bari lowecased but Brindisi-Vlore capitalized?
0
 
LVL 5

Author Comment

by:magento
ID: 39718311
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
0
 
LVL 31

Assisted Solution

by:Marco Gasi
Marco Gasi earned 100 total points
ID: 39718313
Then:
$lines = file('textfile.txt');
foreach ($lines as $line) {
    $urlline = strtolower(str_replace(' ', '-', $line));
    echo "<a title=\"$line\" href=\"http://www.test.fr/$urlline\">$line</a><br />";
}

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
ID: 39718314
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>}'
0
 
LVL 5

Author Comment

by:magento
ID: 39718317
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?
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 250 total points
ID: 39718318
$\=$/;
use utf8;
use charnames qw':full';
while( <> ){
    chomp;
    $s = $_;
    utf8::decode($_);
    s/\s/-/g;
    s[([^\0-\177])][charnames::string_vianame((charnames::viacode(ord $1) =~ /(.*?)( WITH )?/)[0]);]eg;   #convert LATIN SMALL LETTER E WITH DIAERESIS to LATIN SMALL LETTER E
    $_ = qq[<a title="$s" href="http://www.test.fr/\L$_\E/">$_</a>];
}
continue {
    die "-p destination: $!\n" unless print;
}
0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 150 total points
ID: 39718775
Here is a PHP solution.  You want to be aware of the character encoding.  You can use "view source" to see the work product.
http://www.laprbass.com/RAY_temp_magento.php

<?php // RAY_temp_magento.php
error_reporting(E_ALL);


// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28318132.html
// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11880-Unicode-PHP-and-Character-Collisions.html

/* FROM THE POST AT EE
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>
*/

// TEST DATA
$arr = array
( 'Bari Durrës Bari'
, 'Brindisi Vlore'
)
;

// TRANSLATION PROCESS
$out = array();
foreach ($arr as $str)
{
    $uri = strtolower(mungstring($str));
    $uri = str_replace(' ', '-', $uri);
    $out[]
    = '<a title="' . $str . '" '
    . 'href="http://www.test.fr/'
    . $uri
    . '/">'
    . $str
    . '</a>'
    ;
}

// SHOW THE WORK PRODUCT
var_dump($arr, $out);


// A FUNCTION TO RETURN THE WESTERNIZED/ENTITIZED STRING
function mungString($str, $return='TEXT')
{
    // OUR REPLACEMENT ARRAY OF ENTITIES
    static
    $entity
    = array();

    // OUR REPLACEMENT ARRAY OF UTF-8 CHARACERS
    static
    $utf8
    = array();

    // OUR REPLACEMENT ARRAY OF CHARACTERS (YOU MAY WANT SOME CHANGES HERE)
    static
    $normal
    = array
    ( 'ƒ' => 'f'  // http://en.wikipedia.org/wiki/%C6%91 florin
    , 'Š' => 'S'  // http://en.wikipedia.org/wiki/%C5%A0 S-caron (voiceless postalveolar fricative)
    , 'š' => 's'  // http://en.wikipedia.org/wiki/%C5%A0 s-caron
    , 'Ð' => 'Dh' // http://en.wikipedia.org/wiki/Eth (voiced dental fricative)
    , 'Ž' => 'Z'  // http://en.wikipedia.org/wiki/%C5%BD Z-caron (voiced postalveolar fricative)
    , 'ž' => 'z'  // http://en.wikipedia.org/wiki/%C5%BD z-caron
    , 'À' => 'A'
    , 'Á' => 'A'
    , 'Â' => 'A'
    , 'Ã' => 'A'
    , 'Ä' => 'A'
    , 'Å' => 'A'
    , 'Æ' => 'E'
    , 'Ç' => 'C'
    , 'È' => 'E'
    , 'É' => 'E'
    , 'Ê' => 'E'
    , 'Ë' => 'E'
    , 'Ì' => 'I'
    , 'Í' => 'I'
    , 'Î' => 'I'
    , 'Ï' => 'I'
    , 'Ñ' => 'N'
    , 'Ò' => 'O'
    , 'Ó' => 'O'
    , 'Ô' => 'O'
    , 'Õ' => 'O'
    , 'Ö' => 'O'
    , 'Ø' => 'O'
    , 'Ù' => 'U'
    , 'Ú' => 'U'
    , 'Û' => 'U'
    , 'Ü' => 'U'
    , 'Ý' => 'Y'
    , 'Þ' => 'Th' // http://en.wikipedia.org/wiki/Thorn_%28letter%29 (Capital Thorn is smaller)
    , 'ß' => 'Ss'
    , 'à' => 'a'
    , 'á' => 'a'
    , 'â' => 'a'
    , 'ã' => 'a'
    , 'ä' => 'a'
    , 'å' => 'a'
    , 'æ' => 'e'
    , 'ç' => 'c'
    , 'è' => 'e'
    , 'é' => 'e'
    , 'ê' => 'e'
    , 'ë' => 'e'
    , 'ì' => 'i'
    , 'í' => 'i'
    , 'î' => 'i'
    , 'ï' => 'i'
    , 'ð' => 'dh'  // http://en.wikipedia.org/wiki/Eth
    , 'ñ' => 'n'
    , 'ò' => 'o'
    , 'ó' => 'o'
    , 'ô' => 'o'
    , 'õ' => 'o'
    , 'ö' => 'o'
    , 'ø' => 'o'
    , 'ù' => 'u'
    , 'ú' => 'u'
    , 'û' => 'u'
    , 'ý' => 'y'
    , 'ý' => 'y'
    , 'þ' => 'th' // http://en.wikipedia.org/wiki/Thorn_%28letter%29
    , 'ÿ' => 'y'
    )
    ;

    // THE EXPECTED RETURN
    $r = strtoupper(substr($return,0,1));

    // RETURN THE "TRANSLATED" TEXT
    if ($r == 'T') return strtr($str, $normal);

    // RETURN THE "ENTITIZED" TEXT
    if ($r == 'E')
    {
        if (empty($entity))
        {
            foreach ($normal as $key => $nothing)
            {
                $entity[$key] = '&#' . ord($key) . ';';
            }
        }
        return strtr($str, $entity);
    }

    // RETURN THE UTF-8 TEXT
    if ($r == 'U')
    {
        if (empty($utf8))
        {
            foreach ($normal as $key => $nothing)
            {
                $utf8[$key] = utf8_encode($key);
            }
        }
        return strtr($str, $utf8);
    }

    // MIGHT BE USEFUL TO GET THE LIST OF ORIGINAL LETTERS
    return array_keys($normal);
}

Open in new window

0
 
LVL 5

Author Comment

by:magento
ID: 39718983
Thanks everyone for your kind help.

I will close this question sooner.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question