Solved

Q about substr

Posted on 2008-10-12
9
238 Views
Last Modified: 2012-05-05
hi

$pharse1 is a substring of $text,
how can I get the the 12 cases before phrase1?
(there is only one phrase1 in $text)

regards
0
Comment
Question by:uk1900
[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
  • 4
  • 3
  • 2
9 Comments
 
LVL 9

Expert Comment

by:khaledf
ID: 22700229
your question is not clear enough.
can you explain this question by example?
"how can I get the the 12 cases before phrase1?"
0
 

Author Comment

by:uk1900
ID: 22700386
lets say
$text =  "abcdefghijklmnopqrstuvqxyz";
$phrase1 = "mnopq";

how can I get the 12 letter before "mnopq" in $text;
0
 
LVL 27

Accepted Solution

by:
ddrudik earned 400 total points
ID: 22700392
For the 12 letters prior:
<?php
$phrase1="test";
$text="43210987654321test123456";
echo substr($text,strpos($text,$phrase1)-12,12);
?>

Open in new window

0
Industry Leaders: 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!

 
LVL 27

Expert Comment

by:ddrudik
ID: 22700403
String functions would be preferred over regex functions, but here's another one of the ways to match these 12 characters:
<?php
$phrase1="test";
$text="43210987654321test123456";
preg_match("/.{12}(?=$phrase1)/",$text,$match);
echo $match[0];
?>

Open in new window

0
 

Author Comment

by:uk1900
ID: 22700549
both works,
which one is has the best speed performence when it comes to 100s of loops?
0
 
LVL 9

Assisted Solution

by:khaledf
khaledf earned 100 total points
ID: 22702829
to check which one is best for performance, put the code between these 2 php blocks

<?  $time = microtime(true);  ?>


// YOUR CODE HERE

<?  
$time2 = microtime(true);  
$gentime = $time2 - $time;
echo "$gentime";
?>

and see which one is faster
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 22703066
uk1900, it would be expected that string functions (substr etc.) are always faster than regex functions, regex functions are best used when string functions are not available for the specific task.  I just thought I would show another way to do it.
0
 

Author Closing Comment

by:uk1900
ID: 31505532
thank you for your help
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 22707697
Thanks for the question and the points.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with mod_substitute 18 80
Horizontal Full Calendar using php 5 45
phpmyadmin memory error 55 96
php time 12 30
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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 dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

710 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