[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Easy script

Posted on 2013-12-13
11
Medium Priority
?
304 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 400 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 1000 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 111

Accepted Solution

by:
Ray Paseur earned 600 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
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…
Suggested Courses

656 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