I’m having problems with this code, and the PHP method ‘substr’ is playing up. I just don’t get it. Here’s a quick introduction what I’m trying to achieve. I have this massive XML-document with email-subscribers from Joomla. I’m trying to import it to Mailchimp, but Mailchimp have some rules for the syntax of the ways to import emails to a list. So at the moment the syntax is like this:
<subscriber>
<subscriber_id>615</subscriber_id>
<name><![CDATA[NAME OF SUBSCRIBER]]></name>
<email>THE_EMAIL@SOMETHING.COM</email>
<confirmed>1</confirmed>
<subscribe_date>THE DATE</subscribe_date>
</subscriber>
I want to make a simple PHP-script that takes all those emails and outputs them like this:
[THE_EMAIL@SOMETHING.COM] [NAME OF SUBSCRIBER]
[THE_EMAIL@SOMETHING.COM] [NAME OF SUBSCRIBER]
[THE_EMAIL@SOMETHING.COM] [NAME OF SUBSCRIBER]
[THE_EMAIL@SOMETHING.COM] [NAME OF SUBSCRIBER]
If I can do that, then I can just copy paste it into Mailchimp.
Now here’s my PHP-script, so far:
$fileName = file_get_contents('emails.txt');
foreach(preg_split("/((\r?\n)|(\r\n?))/", $fileName) as $line){
if(strpos($line, '<name><![CDATA[')){
$name = strpos($line, '<name><![CDATA[');
$nameEnd = strpos($line, ']]></name>', $name);
$nameLength = $nameEnd-$name;
echo "<br />";
echo " " . strlen(substr($line, $name, $nameLength));
echo " " . gettype(substr($line, $name, $nameLength));
echo " " . substr($line, $name, $nameLength);
}
if(strpos($line, '<email>')){
$var1 = strpos($line, '<email>');
$var2 = strpos($line, '</email>', $var1);
$length = $var2-$var1;
echo substr($line, $var1, $length);
}
}
The first if-statement works as it should. It identifies, if there’s an ”-tag on the line, and if there is, then it finds the end-tag and outputs the email with the substr-method.
The second if-statement is annoying me. If should do the same thing as the first if-statement, but it doesn’t. The length is the correct length (I’ve checked). The type is the correct type (I’ve checked). But when I try to echo it, then nothing happens. The script still runs, but it doesn’t write anything.
I’ve played around with it quite a lot and seem to have tried everything – but I can’t figure it out.
So there’s a few things wrong with this, including the
strposcommand which will actually return0if it finds the tag at the beginning of the line, which doesn’t appear to be what you intend.Also, if the XML is not formatted exactly as you have, with each opening and closing tag on the one line, then your logic will fail as well.
It’s not a good idea to re-invent XML processing for this reason…
Here as others have proposed, is a better solution to the problem*.
*This assumes that you XML is valid, i.e. “subscriber” blocks are contained in a single parent at the top level. You can of course use simplexml documentation to adjust for your use case.