Solved

splitting sentence at a specific word

Posted on 2006-06-21
11
237 Views
Last Modified: 2008-03-10
ive got  a sentence like:  "Adobe Acrobat 6.0 Professional Ver: 006.000.000"

how can i split this sentence at the word "Ver" so:

$list = array();
$list['title'] = "Adobe Acrobat 6.0 Professional"
$list['version'] = "006.000.000"
0
Comment
Question by:ellandrd
  • 6
  • 5
11 Comments
 
LVL 36

Expert Comment

by:Zyloch
ID: 16952818
Hi ellandrd,

You can just use explode, for instance:

$list = array();
$list = explode(' Ver: ', $sentence);
print_r($list);

although it will be $list[0] and $list[1] and not $list['title'] and $list['version'] like you have it.

Regards,
Ted
0
 
LVL 16

Author Comment

by:ellandrd
ID: 16952852
i just get:

Array
(
    [0] =>
)
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 16952874
Did you assign Adobe Acrobat 6.0 Professional Ver: 006.000.000 to $sentence?
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 16

Author Comment

by:ellandrd
ID: 16952889
well ive got all my sentences in an array..

$list = file("list.txt");

$array_size = sizeof($list);                                        

for($i = 0; $i < $array_size; $i++)
{
    $list[$i] = trim(str_replace("\n","",$list[$i]));
}

$list = array_filter($list);

for($i = 0; $i < $array_size; $i++)
{
      $list = explode(' Ver: ', $list[$i]);
}

echo "<pre>";
print_r($list);
echo "</pre>";
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 16952907
Give me a few lines on the file you are grabbing from. You might only need to split a certain line (maybe the first, for instance).
0
 
LVL 16

Author Comment

by:ellandrd
ID: 16952931
here is my txt file contents:

Adobe Reader 7.0.7 Ver: 7.0.7
Alchemy Workstation Suite      
Autodesk DWG Viewer   Ver: 16.1.63.106
CCleaner (remove only)      
Data Access Objects (DAO) 3.5      
J2SE Runtime Environment 5.0 Update 7 Ver: 1.5.0.70
Mathcad 13 Ver: 13.1.3.0
McAfee VirusScan Ver: 4.5.1
Microsoft .NET Framework 1.1 Ver: 1.1.4322

basically what i want to do is split each line at Ver, saving for example "Adobe Reader 7.0.7" into $part[0] and saving "7.0.7" into $part[1] in an array...
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 16953060
For every line that has Ver, this will work:

$list = file("list.txt");
$parts = array();

foreach ($list as $v)
{
    $parts[] = explode(' Ver: ', $v);
}

print_r($parts);
0
 
LVL 16

Author Comment

by:ellandrd
ID: 16953079
ok, but some lines dont have the word Ver.. what happens then? how can i overcome that?
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 16953092
Do you want to just ignore the lines without Ver?
0
 
LVL 16

Author Comment

by:ellandrd
ID: 16953138
yes
0
 
LVL 36

Accepted Solution

by:
Zyloch earned 500 total points
ID: 16953161
foreach ($list as $v)
{
    if (false !== strpos($v, ' Ver: '))
    {
        $parts[] = explode(' Ver: ', $v);
    }
}
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Blocking submission of a web-based form 6 20
google map tracking website 5 22
Using javascript confirmation for a delete in mysql 5 30
Attach to file (img) a unique id 8 24
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question