Solved

Easy script

Posted on 2013-12-13
11
292 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 
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 110

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Six Sigma Control Plans

688 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