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
Solved

trimming strings

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

856 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