PHP, cURL, strpos - Compare substring from text file line by line with string from variable
i get data with cURL from a database and save it in a variable ($alter).
Next, a text file is read. This is done with a loop a certain number of times. Thereby a line is read from a textfile and written to the variable "$linex". The variable "$linex" should then be compared with the variable "$alter. Then the comparison with the second line takes place and so on.
But unfortunately it doesn't work as described. A "false" is always output for every line, even if the string has to match.
Where is the error in the code?
<?php
header('Content-type: text/html; charset=utf-8');
$User_Agent = 'Mozilla/5.0 (Windows NT 6.1; rv:60.0) Gecko/20100101 Firefox/60.0';
$id = $_POST["id"];
$id2 = $_POST["klass"];
$id3 = $_POST["element"];
$url2 = "https://bpk.bs.picturemaxx.com/api/v1/editing/classifications/$id2/elements";
$request_headers = ;
$request_headers = 'Accept: application/json';
$request_headers = 'charset=utf-8';
$request_headers = 'Content-Type: application/json; charset=utf-8';
$request_headers = 'Accept-Encoding: gzip, deflate, identity';
$request_headers = 'Accept-Language: de,en-US;q=0.7,en;q=0.3';
$request_headers = 'X-picturemaxx-api-key: key';
$request_headers = 'Authorization: Bearer key';
$ch = curl_init($url2);
curl_setopt($ch, CURLOPT_USERAGENT, $User_Agent);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_ENCODING, "");
$result = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$data = json_decode($result, true);
$alternativnameze = array();
foreach($data['items'] as $alternativ) {
$alternativname = $alternativ['localized'];
$alternativnamez = $alternativname['de-de'];
$alternativnameze = $alternativnamez['classification_element_name'];
}
$alter = substr(implode($alternativnameze),0 ,1000000);
$file = 'name_test.txt';
$fh = fopen($file, 'r');
if ($fh === false) {
die('Could not open file: '.$file);
}
for ($i = 0; $i < 6; $i++) {
$linex = fgets($fh);
if (strpos($alter, $linex) !== false) {
echo 'true<br />';
} else {
echo 'false<br />';
}
}
if (fclose($fh) === false) {
die('Could not close file: '.$file);
}
?>
Outoput example for variable "$alter"
C. S. Lewis, [29.11.1898 - 22.11.1963], (Clive Staples-Lewis ; Clive
Staples Lewis ; C. S. Luis ; C. Hamilton ; C. S. Ruisu ; Klajv S.
Lʹjuis ; Klaĭv S. Lʹi︠u︡is ; Clive S. Lewis ; Clive Staples Lewis ;
Jack Lewis ; C.S. Luis ; N.W. Clerk ; C.S. Lewis)
Output example for variable "$linex" which should be compared as a substring with the whole string "$alter"
C. S. Lewis, [29.11.1898 - 22.11.1963]
Many thanks in advance for all hints and solution suggestions.
php loops curl strpos
add a comment |
i get data with cURL from a database and save it in a variable ($alter).
Next, a text file is read. This is done with a loop a certain number of times. Thereby a line is read from a textfile and written to the variable "$linex". The variable "$linex" should then be compared with the variable "$alter. Then the comparison with the second line takes place and so on.
But unfortunately it doesn't work as described. A "false" is always output for every line, even if the string has to match.
Where is the error in the code?
<?php
header('Content-type: text/html; charset=utf-8');
$User_Agent = 'Mozilla/5.0 (Windows NT 6.1; rv:60.0) Gecko/20100101 Firefox/60.0';
$id = $_POST["id"];
$id2 = $_POST["klass"];
$id3 = $_POST["element"];
$url2 = "https://bpk.bs.picturemaxx.com/api/v1/editing/classifications/$id2/elements";
$request_headers = ;
$request_headers = 'Accept: application/json';
$request_headers = 'charset=utf-8';
$request_headers = 'Content-Type: application/json; charset=utf-8';
$request_headers = 'Accept-Encoding: gzip, deflate, identity';
$request_headers = 'Accept-Language: de,en-US;q=0.7,en;q=0.3';
$request_headers = 'X-picturemaxx-api-key: key';
$request_headers = 'Authorization: Bearer key';
$ch = curl_init($url2);
curl_setopt($ch, CURLOPT_USERAGENT, $User_Agent);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_ENCODING, "");
$result = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$data = json_decode($result, true);
$alternativnameze = array();
foreach($data['items'] as $alternativ) {
$alternativname = $alternativ['localized'];
$alternativnamez = $alternativname['de-de'];
$alternativnameze = $alternativnamez['classification_element_name'];
}
$alter = substr(implode($alternativnameze),0 ,1000000);
$file = 'name_test.txt';
$fh = fopen($file, 'r');
if ($fh === false) {
die('Could not open file: '.$file);
}
for ($i = 0; $i < 6; $i++) {
$linex = fgets($fh);
if (strpos($alter, $linex) !== false) {
echo 'true<br />';
} else {
echo 'false<br />';
}
}
if (fclose($fh) === false) {
die('Could not close file: '.$file);
}
?>
Outoput example for variable "$alter"
C. S. Lewis, [29.11.1898 - 22.11.1963], (Clive Staples-Lewis ; Clive
Staples Lewis ; C. S. Luis ; C. Hamilton ; C. S. Ruisu ; Klajv S.
Lʹjuis ; Klaĭv S. Lʹi︠u︡is ; Clive S. Lewis ; Clive Staples Lewis ;
Jack Lewis ; C.S. Luis ; N.W. Clerk ; C.S. Lewis)
Output example for variable "$linex" which should be compared as a substring with the whole string "$alter"
C. S. Lewis, [29.11.1898 - 22.11.1963]
Many thanks in advance for all hints and solution suggestions.
php loops curl strpos
I'd be willing to bet there arenewlinecharacters or similar messing you up. Try$linex = rtrim($linex, "rn");
– Andy Foster
Nov 26 '18 at 16:12
Thank you Andy Foster. It is working now :)
– SIB
Nov 26 '18 at 16:16
Suppose I should make it an actual answer then.
– Andy Foster
Nov 26 '18 at 16:17
add a comment |
i get data with cURL from a database and save it in a variable ($alter).
Next, a text file is read. This is done with a loop a certain number of times. Thereby a line is read from a textfile and written to the variable "$linex". The variable "$linex" should then be compared with the variable "$alter. Then the comparison with the second line takes place and so on.
But unfortunately it doesn't work as described. A "false" is always output for every line, even if the string has to match.
Where is the error in the code?
<?php
header('Content-type: text/html; charset=utf-8');
$User_Agent = 'Mozilla/5.0 (Windows NT 6.1; rv:60.0) Gecko/20100101 Firefox/60.0';
$id = $_POST["id"];
$id2 = $_POST["klass"];
$id3 = $_POST["element"];
$url2 = "https://bpk.bs.picturemaxx.com/api/v1/editing/classifications/$id2/elements";
$request_headers = ;
$request_headers = 'Accept: application/json';
$request_headers = 'charset=utf-8';
$request_headers = 'Content-Type: application/json; charset=utf-8';
$request_headers = 'Accept-Encoding: gzip, deflate, identity';
$request_headers = 'Accept-Language: de,en-US;q=0.7,en;q=0.3';
$request_headers = 'X-picturemaxx-api-key: key';
$request_headers = 'Authorization: Bearer key';
$ch = curl_init($url2);
curl_setopt($ch, CURLOPT_USERAGENT, $User_Agent);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_ENCODING, "");
$result = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$data = json_decode($result, true);
$alternativnameze = array();
foreach($data['items'] as $alternativ) {
$alternativname = $alternativ['localized'];
$alternativnamez = $alternativname['de-de'];
$alternativnameze = $alternativnamez['classification_element_name'];
}
$alter = substr(implode($alternativnameze),0 ,1000000);
$file = 'name_test.txt';
$fh = fopen($file, 'r');
if ($fh === false) {
die('Could not open file: '.$file);
}
for ($i = 0; $i < 6; $i++) {
$linex = fgets($fh);
if (strpos($alter, $linex) !== false) {
echo 'true<br />';
} else {
echo 'false<br />';
}
}
if (fclose($fh) === false) {
die('Could not close file: '.$file);
}
?>
Outoput example for variable "$alter"
C. S. Lewis, [29.11.1898 - 22.11.1963], (Clive Staples-Lewis ; Clive
Staples Lewis ; C. S. Luis ; C. Hamilton ; C. S. Ruisu ; Klajv S.
Lʹjuis ; Klaĭv S. Lʹi︠u︡is ; Clive S. Lewis ; Clive Staples Lewis ;
Jack Lewis ; C.S. Luis ; N.W. Clerk ; C.S. Lewis)
Output example for variable "$linex" which should be compared as a substring with the whole string "$alter"
C. S. Lewis, [29.11.1898 - 22.11.1963]
Many thanks in advance for all hints and solution suggestions.
php loops curl strpos
i get data with cURL from a database and save it in a variable ($alter).
Next, a text file is read. This is done with a loop a certain number of times. Thereby a line is read from a textfile and written to the variable "$linex". The variable "$linex" should then be compared with the variable "$alter. Then the comparison with the second line takes place and so on.
But unfortunately it doesn't work as described. A "false" is always output for every line, even if the string has to match.
Where is the error in the code?
<?php
header('Content-type: text/html; charset=utf-8');
$User_Agent = 'Mozilla/5.0 (Windows NT 6.1; rv:60.0) Gecko/20100101 Firefox/60.0';
$id = $_POST["id"];
$id2 = $_POST["klass"];
$id3 = $_POST["element"];
$url2 = "https://bpk.bs.picturemaxx.com/api/v1/editing/classifications/$id2/elements";
$request_headers = ;
$request_headers = 'Accept: application/json';
$request_headers = 'charset=utf-8';
$request_headers = 'Content-Type: application/json; charset=utf-8';
$request_headers = 'Accept-Encoding: gzip, deflate, identity';
$request_headers = 'Accept-Language: de,en-US;q=0.7,en;q=0.3';
$request_headers = 'X-picturemaxx-api-key: key';
$request_headers = 'Authorization: Bearer key';
$ch = curl_init($url2);
curl_setopt($ch, CURLOPT_USERAGENT, $User_Agent);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_ENCODING, "");
$result = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$data = json_decode($result, true);
$alternativnameze = array();
foreach($data['items'] as $alternativ) {
$alternativname = $alternativ['localized'];
$alternativnamez = $alternativname['de-de'];
$alternativnameze = $alternativnamez['classification_element_name'];
}
$alter = substr(implode($alternativnameze),0 ,1000000);
$file = 'name_test.txt';
$fh = fopen($file, 'r');
if ($fh === false) {
die('Could not open file: '.$file);
}
for ($i = 0; $i < 6; $i++) {
$linex = fgets($fh);
if (strpos($alter, $linex) !== false) {
echo 'true<br />';
} else {
echo 'false<br />';
}
}
if (fclose($fh) === false) {
die('Could not close file: '.$file);
}
?>
Outoput example for variable "$alter"
C. S. Lewis, [29.11.1898 - 22.11.1963], (Clive Staples-Lewis ; Clive
Staples Lewis ; C. S. Luis ; C. Hamilton ; C. S. Ruisu ; Klajv S.
Lʹjuis ; Klaĭv S. Lʹi︠u︡is ; Clive S. Lewis ; Clive Staples Lewis ;
Jack Lewis ; C.S. Luis ; N.W. Clerk ; C.S. Lewis)
Output example for variable "$linex" which should be compared as a substring with the whole string "$alter"
C. S. Lewis, [29.11.1898 - 22.11.1963]
Many thanks in advance for all hints and solution suggestions.
php loops curl strpos
php loops curl strpos
edited Nov 26 '18 at 15:51
RiggsFolly
70.8k1864111
70.8k1864111
asked Nov 26 '18 at 15:47
SIBSIB
189
189
I'd be willing to bet there arenewlinecharacters or similar messing you up. Try$linex = rtrim($linex, "rn");
– Andy Foster
Nov 26 '18 at 16:12
Thank you Andy Foster. It is working now :)
– SIB
Nov 26 '18 at 16:16
Suppose I should make it an actual answer then.
– Andy Foster
Nov 26 '18 at 16:17
add a comment |
I'd be willing to bet there arenewlinecharacters or similar messing you up. Try$linex = rtrim($linex, "rn");
– Andy Foster
Nov 26 '18 at 16:12
Thank you Andy Foster. It is working now :)
– SIB
Nov 26 '18 at 16:16
Suppose I should make it an actual answer then.
– Andy Foster
Nov 26 '18 at 16:17
I'd be willing to bet there are
newline characters or similar messing you up. Try $linex = rtrim($linex, "rn");– Andy Foster
Nov 26 '18 at 16:12
I'd be willing to bet there are
newline characters or similar messing you up. Try $linex = rtrim($linex, "rn");– Andy Foster
Nov 26 '18 at 16:12
Thank you Andy Foster. It is working now :)
– SIB
Nov 26 '18 at 16:16
Thank you Andy Foster. It is working now :)
– SIB
Nov 26 '18 at 16:16
Suppose I should make it an actual answer then.
– Andy Foster
Nov 26 '18 at 16:17
Suppose I should make it an actual answer then.
– Andy Foster
Nov 26 '18 at 16:17
add a comment |
2 Answers
2
active
oldest
votes
Try $linex = rtrim($linex, "rn"); prior to the comparison to handle extra line-ending characters which are likely in your file.
add a comment |
fgets returns a line from the file with the new line chararcter. So if a line in your text file is just an a, fgets($fh); will return an (in Linux, different character on other OS). strpos("a b c", "an") will always returns false.
A trim should do the trick:
$linex = trim(fgets($fh));
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53484679%2fphp-curl-strpos-compare-substring-from-text-file-line-by-line-with-string-fr%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try $linex = rtrim($linex, "rn"); prior to the comparison to handle extra line-ending characters which are likely in your file.
add a comment |
Try $linex = rtrim($linex, "rn"); prior to the comparison to handle extra line-ending characters which are likely in your file.
add a comment |
Try $linex = rtrim($linex, "rn"); prior to the comparison to handle extra line-ending characters which are likely in your file.
Try $linex = rtrim($linex, "rn"); prior to the comparison to handle extra line-ending characters which are likely in your file.
answered Nov 26 '18 at 16:18
Andy FosterAndy Foster
11018
11018
add a comment |
add a comment |
fgets returns a line from the file with the new line chararcter. So if a line in your text file is just an a, fgets($fh); will return an (in Linux, different character on other OS). strpos("a b c", "an") will always returns false.
A trim should do the trick:
$linex = trim(fgets($fh));
add a comment |
fgets returns a line from the file with the new line chararcter. So if a line in your text file is just an a, fgets($fh); will return an (in Linux, different character on other OS). strpos("a b c", "an") will always returns false.
A trim should do the trick:
$linex = trim(fgets($fh));
add a comment |
fgets returns a line from the file with the new line chararcter. So if a line in your text file is just an a, fgets($fh); will return an (in Linux, different character on other OS). strpos("a b c", "an") will always returns false.
A trim should do the trick:
$linex = trim(fgets($fh));
fgets returns a line from the file with the new line chararcter. So if a line in your text file is just an a, fgets($fh); will return an (in Linux, different character on other OS). strpos("a b c", "an") will always returns false.
A trim should do the trick:
$linex = trim(fgets($fh));
answered Nov 26 '18 at 16:18
drmaddrmad
1467
1467
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53484679%2fphp-curl-strpos-compare-substring-from-text-file-line-by-line-with-string-fr%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I'd be willing to bet there are
newlinecharacters or similar messing you up. Try$linex = rtrim($linex, "rn");– Andy Foster
Nov 26 '18 at 16:12
Thank you Andy Foster. It is working now :)
– SIB
Nov 26 '18 at 16:16
Suppose I should make it an actual answer then.
– Andy Foster
Nov 26 '18 at 16:17