php read list from website into array - php

I have a list of products codes that i wish to read into an array using php.
the list is to be fetched from a website and has over 700 items looks something like this:
4310ABC
4590DEF
8950GHK
What i want to do is put every code into a php array like so:
php_array ( [0] => 4310ABC
[1] => 4590DEF
[2] => 8950GHK)
This is what i have:
$php_array = file_get_contents('http://anysite.net/product_codes.php');
print_r (explode("\n",$php_array));
But my result is :
Array ( [0] => 4310ABC
4590DEF
8950GHK)
I have tried explode, preg_split('/[\n\r]+/', $php_array); but nothing seems to do the trick. Can anyone give me some pointers? thanks!

The lines are separated by a br, so use this instead:
$php_array = file_get_contents('http://anysite.net/product_codes.php');
print_r (explode("<br>",$php_array));
Don't forget to change the br to however it's spelled within the document you are fetching, for example it's often spelled like this:
<br />
Which is the most correct way to write it.

It would depend how your php file is echoing out the three values, so I am not sure how it is interpreting line breaks. Try echoing out the values with no line breaks, but separated by some other character like '*' or something, and then explode them along that and see if that works.

Related

TextArea joins as one string to PHP how can I set the other strings in new line with help of delimiter

I've tried searching all over google and tried a similar problem and answers yet I still didn't get my problem done.
So basically, I'm making an HTML to write user input in a textbox which is the user would input:
Dog,
cat,
coronavirus,
Fever,
Cough,
Then into my PHP code, I capture it with:
$input = $_GET['contents'];
So I tried this one which I saw from googling and it doesn't have an explanation but logically its about array:
$input= explode(array(",", ""), $input)[0];
It works but the problem is that it only shows the first output which is:
Dog
Dog
Dog
Dog
Dog
But when I tried to remove the delimiter code, it shows one string only:
Dog,cat,coronavirus,Fever,Cough,
I thought about it something related to array but I'm not sure, is there any way to display all of them per line and with the help with delimiter?
UPDATE:
So I tried doing print_r($secs); and it shows like this:
Array ( [0] => Dog [1] => cat )
Array ( [0] => )
My accomplishment is to read all of the arrays one by one in another line instead of joining it as one strings. Sorry for bad english and im trying my best to explain.
you do not need the array() line.
Instead
if(strpos($_GET['contents'],',') !== false) { //check for commas first
$input = explode(',' , $_GET['contents']);
echo '<pre>';
print_r($input); //gives you the array
echo '</pre>';
}
to access each string individually, call the offset. For example, print_r($input[0]);
Also a friendly piece of advice. You seem like a beginner. You do not have to execute multiple commands in one line. Write your code step by step.

Setting filterValues from HTML input in a Marketo API with PHP

I am trying to pass text strings into an array so that they can be used as a filterValue in Marketo's REST API via PHP.
Users submit their form while listing each element every line. That goes into an array. I can successfully convert this to an array. For example, if the HTML form was filled
item1
item2
item3
and I print_r() the output, it looks like:
Array ( [0] => item1 [1] => item2 [2] => item3 )
So that sems to be working. Let's say I save that as $output. However, if I pass $output to $leads->filterValues, it does not work. I've tried simply making an array and passing it directly or calling a function like so:
$leads->filterValues = $output;
or
$leads->filterValues = setValues();
Neither of which work and the API call fails. However, if I hardcode these items in like so
$leads->filterValues = array("item1", "item2", "item3");
Everything works just fine then, and the API call goes through with those values. What's going on here? Is there a way I can pass the input data and set that to the filterValues array?
Turns out, the URL from the data from the form had way more characters then the URL from hardcoded data when using var_dump() despite being identical. Use
$url = preg_replace('/\s+/', ' ', $url);
before curl_init($url) to cleanse it of the hidden characters.

getCsvControl() always returns same delimiter - PHP

$file = new SplFileObject('D:\BackUp\addressbook.csv');
print_r($file->getCsvControl());
What i am trying to do is find the delimiter of a csv file using php. the addressbook.csv file looks like
"id";"firstname";"lastname";"phone";"email"
"1";"jishan";"ishrak";"17878";"jishan.ishrak#gmail.com"
and another file is addressbook1.csv which is like
"id","firstname","lastname","phone","email"
"1","jishan","ishrak","17878","jishan.ishrak#gmail.com"
one is separated by "," and another one is with ";" but the function
getCsvControl()
always returns an array like
Array ( [0] => , [1] => " )
I mean in the [0] index it always gives "," for both files
is there a way to solve this issue.
This is not a bug. SplFileObject::getCsvControl() is never intended to detect the delimiter from a CSV file. It returns only the default control characters or the one previously set with SplFileObject::setCsvControl(). And this set CSV control characters are used, if is nothing handed over in the SplFileObject::fgetcsv() method.
Ok, it's badly documented, but this were my first thoughts, the method would never detect the characters and a look into the php source code confirmed this.
Proabably this is a bug?
as you can see here php doc 1st comment 1 year ago - Seems that this function always returns the same delimiter.
UPDATE
this is not a bug look at Pazi ツ answer.

PHP json_decode + Print Array Values Fails

stdClass Object ( [free] => 100% δωρεάν [meetsingles] => Γνωρίστε Singles [searchprofiles] => Προφίλ Αναζήτηση )
I have a JSON array that after decoded 'json_decode' and printed to screen it looks like above - using UTF8 for Greek.
This is how I print it:
$siteLanguages = json_decode($result);
print_r($siteLanguages);
When I try to access one of the values the page displays only until the point of the print and then it stops loading - eg: like half a page will show - comment this out and the whole page shows - below is how I'm trying:
print $siteLanguages['searchprofiles'];
I can't see why I can't use the associate array like any other.
Is there a trick I'm missing here?
Should the decoded json array show 'stdClass Object' when printed?
thx
I think you're dealing with an object, not array
print $siteLanguages -> searchprofiles;
The right way is this:
$siteLanguages = json_decode($result,true);
will get an array,your way get an object;

Regex not finding all variables

I'm parsing some HTML, that I have generated in a form. This is a token system. I'm trying to get the information from the Regexp later on, but somehow, it's turning up only the first of the matches. I found a regexp on the Web, that did almost what I needed, except of being able to process multiple occurances.
I want to be able to replace the content found, with content that was generated from the found string.
So, here is my code:
$result = preg_replace_callback("/<\/?\w+((\s+(\w|\w[\w-]*\w)(\s*=\s*(?:\".*?\"|'.*?'|[^'\">\s]+))?)+\s*|\s*)\/?>\[\*.*\*\]\<\/[a]\>/i", array(get_class($this), 'embed_video'), $str);
public function embed_video($matches)
{
print_r($matches);
return $matches[1] . 'foo';
}
I really need only the attributes, since they containt all of the valuable information. The contents of the tag are used only to find the token. This is an example of what needs to happen:
<a type="TypeOfToken1" id="IdOfToken1">[*SomeTokenTitle1*]</a>
<a type="TypeOfToken2" id="IdOfToken2">[*SomeTokenTitle2*]</a>
After the preg_replace_callback() this should be returned:
type="TypeOfToken1" id="IdOfToken1" type="TypeOfToken2" id="IdOfToken2"
But, the callback function outputs the matches, but does not replace them with the return. So, the $result stays the same after the preg_replace_callback. What could be the problem?
An example with real data:
Input:
<p><a id="someToken1" rel="someToken1">[*someToken1*]</a> sdfsdf <a id="someToken2" rel="someToken2">[*someToken2*]</a></p>
returned $result:
id="someToken1" rel="someToken1"foo
Return from the print_r() if the callback function:
Array ( [0] => [*someToken1*] sdfsdf [*someToken2*] [1] => id="someToken1" rel="someToken1" [2] => rel="someToken1" [3] => rel [4] => ="someToken1" )
I think that it is not returning both of the strings it should.
For anyone else stumbling into a problem like this, try checking your regexp and it's modifiers.
Regarding the parsing of the document, I'm still doing it, just not HTML tags. I have instead gone with someting more textlike, that can be more easily parsed. In my case: [*TokeName::TokenDetails*].

Categories