Jump to content

How to combine a string to get a foreach & run in a loop


dilbertone

Recommended Posts

Hello dear community, good day!

 

first of all: Merry Christmas to all of you!!

 

 

How to combine / concatenate a *divided* string in order to use this combined / concatenated string in a loop where i run the

 

$dom = new DOMDocument();
@$dom->loadHTMLFile('<- path to the file-> =60119');

 

and the following.... numbers - Note: they replace the ending!!!

 

60299

64643

62958

63678

60419

60585

60749

60962

 

and so on. (

 

Question: How to combine the string (in fact the string is an URL) so that i am able to build the URLs automatically. And that i am able to run all that in a loop - eg with foreach [probably this is the right way to do that].

 

I hope that i was able to explain the question so that you understand it. If i have to be more descriptive - just let me know!

 

Many many thanks for a hint!

 

db1  ;)

 

Link to comment
Share on other sites

hello dear revraz, hello dear litebearer, good day

 

 

many many tanks to you both! Great to hear from you.

The idea with an array is convincing me! I am convinced! 

 

BTW: this uses the Dot-Operator, doesn ´t it!? One of two solutions for string- or Url-concetenation??

 

 

Perhaps like this?

$orig_string = "http://www.somesite.com?page=";
$number_array = array ("123", "43567", "9287","3323");
for($i=0; $i<$count($number_array); $i ++) {
$new_url = $orig_url . $number_array[$i];
/* do something with the new url */
}

 

many many thanks for the hint!!

 

btw; this is a absolute great forum - i love it!!  Many many thanks for the ideas and hints.

 

@ you both - you are very very supportive. GREAT To have you here!

 

Have a great season break and merry merry Christmas

 

greetings

Dilbertone!

 

 

Update: one last question: I integrate the loop solution with the array into my basic-script ....

 

<?php

$dom = new DOMDocument();
@$dom->loadHTMLFile('http://schulen.bildung-rp.de/gehezu/startseite/einzelanzeige.html?tx_wfqbe_pi1%5buid%5d=60119');
$divElement = $dom->getElementById('wfqbeResults');
$innerHTML= '';
$children = $divElement->childNodes;
foreach ($children as $child) {
$innerHTML = $child->ownerDocument->saveXML( $child );

$doc = new DOMDocument();
$doc->loadHTML($innerHTML);
//$divElementNew = $dom->getElementsByTagName('td');
$divElementNew = $dom->getElementsByTagname('td');

    /*** the array to return ***/
    $out = array();
    foreach ($divElementNew as $item)
    {
        /*** add node value to the out array ***/
        $out[] = $item->nodeValue;
    }

echo '<pre>';
print_r($out);
echo '</pre>';

} 

 

 

 

.......like so:

 

 

<?php

$dom = new DOMDocument();
$orig_string = "http://www.somesite.com?page=";

@$dom->loadHTMLFile {

$number_array = array ("123", "43567", "9287","3323");
for($i=0; $i<$count($number_array); $i ++) {

$new_url = $orig_url . $number_array[$i];

/* do something with the new url */
}

$divElement = $dom->getElementById('wfqbeResults');
$innerHTML= '';
$children = $divElement->childNodes;
foreach ($children as $child) {
$innerHTML = $child->ownerDocument->saveXML( $child );

$doc = new DOMDocument();
$doc->loadHTML($innerHTML);
//$divElementNew = $dom->getElementsByTagName('td');
$divElementNew = $dom->getElementsByTagname('td');

    /*** the array to return ***/
    $out = array();
    foreach ($divElementNew as $item)
    {
        /*** add node value to the out array ***/
        $out[] = $item->nodeValue;
    }

echo '<pre>';
print_r($out);
echo '</pre>';

} 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.