Solved

trimming strings

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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.  …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

706 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

18 Experts available now in Live!

Get 1:1 Help Now