Solved

trimming strings

Posted on 2006-06-22
3
200 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses how to create an extensible mechanism for linked drop downs.
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

864 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now