Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8718499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:33:58+00:00 2026-06-13T06:33:58+00:00

I’m having problems with this code, and the PHP method ‘substr’ is playing up.

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T06:34:00+00:00Added an answer on June 13, 2026 at 6:34 am

    So there’s a few things wrong with this, including the strpos command which will actually return 0 if 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*.

    $xml = simplexml_load_file('emails.txt');
    foreach( $xml->subscriber as $sub ) 
    {
      // Note that SimpleXML is aware of CDATA, and only outputs the text
      $output = '[' . $sub->name . ']' . ' ' . '[' . $sub->email . ']'; 
    }
    

    *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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.