Solved

Easy script

Posted on 2013-12-13
11
277 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 30

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 30

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
 
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 30

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 108

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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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 …

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now