Solved

trimming strings

Posted on 2006-06-22
3
206 Views
Last Modified: 2006-11-18
i have 67 strings that can vary in what values they hold.

an example is:

MS 300 Series - Management System Monitoring and Non Conformance Control/MS-301 Internal Audit Report.doc
MS 300 Series - Management System Monitoring and Non Conformance Control/MS-302 - The Corrective Action Process.vsd
MS 300 Series - Management System Monitoring and Non Conformance Control/MS-302 Corrective Action Request.doc
MS 300 Series - Management System Monitoring and Non Conformance Control/MS-303 - Preventive Action.vsd
MS 300 Series - Management System Monitoring and Non Conformance Control/MS-310 - Assessment of Contractors Sht 1.vsd

but i only what to display:

MS-301 Internal Audit Report
MS-302 - The Corrective Action Process
MS-302 Corrective Action Request
MS-303 - Preventive Action
MS-310 - Assessment of Contractors Sht 1

... im not too sure how to trim both the beginning and the end...
0
Comment
Question by:ellandrd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 36

Accepted Solution

by:
Zyloch earned 500 total points
ID: 16962230
Hi ellandrd,

Well, you seem to only want the end, and conveniently, there is a / between them and a . between the extension. You can merely do:

$a_strings = array();
$a_strings[] = 'MS 300 Series - Management System Monitoring and Non Conformance Control/MS-301 Internal Audit Report.doc';
$a_strings[] = 'MS 300 Series - Management System Monitoring and Non Conformance Control/MS-302 - The Corrective Action Process.vsd';
//etc.etc.

$a_parsed_strings = array();
foreach ($a_strings as $v)
{
    $slash_pos = strrpos($v, '/');
    $dot_pos = strrpos($v, '.');
    $a_parsed_strings[] = substr($v, $slash_pos + 1, $dot_pos - $slash_pos - 1);
}

print_r($a_parsed_strings);

Regards,
Ted
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 16962342
Hi, you may also want to try regular expressions...

<?php
$strarr = array();
$strarr[] = 'MS 300 Series - Management System Monitoring and Non Conformance Control/MS-301 Internal Audit Report.doc';
$strarr[] = 'MS 300 Series - Management System Monitoring and Non Conformance Control/MS-302 - The Corrective Action Process.vsd';
// ... add as many strings as required, or get from database..

$req = array();
foreach ($strarr as $str)
{
    preg_match("#/([-\s\w]+)\.#s",$str,$match);
    $req[] = $match[1];
}

print_r($req);
?>


---
Harish
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 16962375
Yep, regular expressions will also work as well, and is probably a better bet if the format of your strings start to become a bit more irregular.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

690 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