Solved

Q about substr

Posted on 2008-10-12
9
233 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
  • 4
  • 3
  • 2
9 Comments
 
LVL 9

Expert Comment

by:khaledf
Comment Utility
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
Comment Utility
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
Comment Utility
For the 12 letters prior:
<?php

$phrase1="test";

$text="43210987654321test123456";

echo substr($text,strpos($text,$phrase1)-12,12);

?>

Open in new window

0
 
LVL 27

Expert Comment

by:ddrudik
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:uk1900
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
thank you for your help
0
 
LVL 27

Expert Comment

by:ddrudik
Comment Utility
Thanks for the question and the points.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

772 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

9 Experts available now in Live!

Get 1:1 Help Now