2

I'm grabbing the source of a page with a lot of PDF links (files) listed in a column like so. simple_php_dom.php

TALB20170826D-$A$$-RA11.pdf
TAP$20170826D-$A$$-RA11.pdf
TASD20170826D-$A$$-RA11.pdf
TAUA20170826D-$A$$-RA11.pdf
TAUB20170826D-$A$$-RA11.pdf
TAUC20170826D-$A$$-RA11.pdf
TAUD20170826D-$A$$-RA11.pdf
TBTP20170826D-$A$$-RA11.pdf
TCBY20170826D-$A$$-RA11.pdf

I need to rename them within the foreach loop

foreach($html->find('a') as $element) 
echo $element->href;

For instance TALB is an abbreviation for ALBANY, TAP is an abbreviation for Asia Pacific and so on.

I have a list of the names that correspond with the abbreviations but unsure how to rename them within the loop? Any help would be greatly appreciated!

1 Answer 1

4

You can put the list of replacements in arrays:

<?php    
/*
$list = [];   
$list[] = "TALB20170826D-\$A$$-RA11.pdf";
$list[] = "TAP$20170826D-\$A$$-RA11.pdf";
$list[] = "TASD20170826D-\$A$$-RA11.pdf";
$list[] = "TAUA20170826D-\$A$$-RA11.pdf";
$list[] = "TAUB20170826D-\$A$$-RA11.pdf";
$list[] = "TAUC20170826D-\$A$$-RA11.pdf";
$list[] = "TAUD20170826D-\$A$$-RA11.pdf";
$list[] = "TBTP20170826D-\$A$$-RA11.pdf";
$list[] = "TCBY20170826D-\$A$$-RA11.pdf";
*/
$list = $html->find('a');
$abbr = [
    "TALB",
    "TAP",
    // ...
];
$replacements = [
    "ALBANY",
    "Asia Pacific",
    // ...
];
foreach ($list as &$el) {
    $el->href = str_replace($abbr, $replacements, $el->href);
}

Demo

Or, to keep them all in one associative array (order doesn't matter and missing items just won't be replaced, no errors):

<?php    
/*$list = [];   
$list[] = "TALB20170826D-\$A$$-RA11.pdf";
$list[] = "TAP$20170826D-\$A$$-RA11.pdf";
$list[] = "TASD20170826D-\$A$$-RA11.pdf";
$list[] = "TAUA20170826D-\$A$$-RA11.pdf";
$list[] = "TAUB20170826D-\$A$$-RA11.pdf";
$list[] = "TAUC20170826D-\$A$$-RA11.pdf";
$list[] = "TAUD20170826D-\$A$$-RA11.pdf";
$list[] = "TBTP20170826D-\$A$$-RA11.pdf";
$list[] = "TCBY20170826D-\$A$$-RA11.pdf";*/
$list = $html->find('a');
$abbr = [
    "TALB" => "ALBANY",
    "TAP" => "Asia Pacific",
];
foreach ($list as &$el) {
    $el->href = strtr($el->href, $abbr);
}

Demo

Or use array_map(), maybe you'll find it a bit cleaner:

$list = array_map(function($el) use($abbr) {
    return strtr($el, $abbr);
}, $list);
Sign up to request clarification or add additional context in comments.

5 Comments

Will this work if the abbreviations aren't in order or sometimes not listed?
It won't with the first way, but I edited in another way of doing it which will do it the way you want it to.
That works! Now how would I get the pdf link names into the $list = [] array? For instance, the date within the string will change everyday I grab the links.
I'm not sure what you mean. Where are you getting the list from? You should post a new question though, to keep things organized.
I've edited, now the $list comes directly from $html->find('a').

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.