473,320 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

break text to sepearte strings in php

116 100+
i have a text line " as title: news words:500 body: this is a great day

now i want the it be broken into such that 3 parts with the variables holding the data as follows

$title = news
$words = 500
$body = this is a great day

how can i get that? i m trying explode implode but not working here's my code

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $first = array();
  3.  
  4. $string = "title: news words:500 body: this is a great day ";
  5. $sep1 ="title: ";
  6. $sep2 ="words: ";
  7. $sep3 ="body: ";
  8.  
  9. $first[0] = explode($sep2, $string);
  10. $string0 = implode("",$first[0]);
  11. $title = $string0;
  12. echo $title;
  13.  
  14. ?>
  15.  
  16.  
  17.  
Jun 21 '10 #1

✓ answered by JKing

@angelicdevil
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $first = array();
  3.  
  4. $string = "title: news words: 500 body: this is a great day ";
  5. $sep1 ="title: ";
  6. $sep2 ="words: ";
  7. $sep3 ="body: ";
  8.  
  9. $body = strstr($string, $sep3);
  10.  
  11.  
  12. $string = str_replace ($body ,"", $string);
  13. $body = str_replace ($sep3, "", $body);
  14. $title = strstr($string, $sep2, true);
  15. $string= str_replace ($title, "" , $string);
  16. $title = str_replace ($sep1, "", $title);
  17. $words= str_replace($sep2, "", $string);
  18.  
  19. echo $title . "<br />";
  20. echo $body . "<br />";
  21. echo $words;
  22.  
  23. ?>

3 1365
JKing
1,206 Expert 1GB
@angelicdevil
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $first = array();
  3.  
  4. $string = "title: news words: 500 body: this is a great day ";
  5. $sep1 ="title: ";
  6. $sep2 ="words: ";
  7. $sep3 ="body: ";
  8.  
  9. $body = strstr($string, $sep3);
  10.  
  11.  
  12. $string = str_replace ($body ,"", $string);
  13. $body = str_replace ($sep3, "", $body);
  14. $title = strstr($string, $sep2, true);
  15. $string= str_replace ($title, "" , $string);
  16. $title = str_replace ($sep1, "", $title);
  17. $words= str_replace($sep2, "", $string);
  18.  
  19. echo $title . "<br />";
  20. echo $body . "<br />";
  21. echo $words;
  22.  
  23. ?>
Jun 21 '10 #2
HaLo2FrEeEk
404 256MB
Instead of actually including the words title:, words:, body:, just leave them out and use a specific separator between your items, so if your title is "news", words is 500 and body is "hello world", try this:

Expand|Select|Wrap|Line Numbers
  1. $string = "News|500|Hello World!";
  2.  
  3. $split = explode("|", $string);
Now your $split variable is an array with these values:

$split[0] = News
$split[1] = 500
$split[2] = Hello World

Use them as you would.
Jun 21 '10 #3
angelicdevil
116 100+
thanx it worked
Jun 24 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: lawrence | last post by:
"Garp" <garp7@no7.blueyonder.co.uk> wrote in message news:<_vpuc.1424$j_3.13346038@news-text.cableinet.net>... > "lawrence" <lkrubner@geocities.com> wrote in message >...
13
by: Daniel Heiserer | last post by:
Hi, I want to fill a string like this: using namespace std; string text=" now a lot of text ................ now a lot of text ................ now a lot of text ................ now a lot...
6
by: Bryan Dickerson | last post by:
I have a string that is delimited, e.g. "SOD;NAME;ADDRESS1;ADDRESS2;PHONE;CITY;STATE;ZIP;EOD", that I need to strip off the "SOD;" at the beginning and the ";EOD" at the end. I am well familiar...
18
by: mouseit101 | last post by:
I'm writing a program to emulate nuerons (can't spell for my life sorry) and am using an array of "mailboxes" to propagate messages through the network, but I need to be able to remove the request...
0
by: flameboy | last post by:
I am currently trying to figure my way how to output strings to a file. I have seen all the many posts and such how to do it in a console program, and have had much success doing it that way. This...
16
by: InDepth | last post by:
Now that .NET is at it's fourth release (3.5 is coming soon), my very humble question to the gurus is: "What have we won with the decision to have string objects immutable? Or did we won?" ...
4
by: deerchao | last post by:
Hi, I'd like to render colorful text with word wrap on screen (kind of inline <fonttag to change text color in html), but run into the line break problem. Graphics provides several DrawString...
3
by: TVining | last post by:
I want to remove all text in a single field that matches "Army & Air Force Hometown News News Release 123 System St. (210)123-3333 Fax: (210)123-2222 San Antonio, TX...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.