Welcome to WebmasterWorld Guest from 54.224.43.96

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

search for string within a string using positions

     
9:37 am on May 22, 2009 (gmt 0)

Junior Member

5+ Year Member

joined:July 8, 2008
posts: 80
votes: 0


say i have a string like the following:

aaa999bbb

how can i extract just 999 here?

tried many fucntions but still no luck.thanks in advance!

9:39 am on May 22, 2009 (gmt 0)

Junior Member

5+ Year Member

joined:July 8, 2008
posts: 80
votes: 0


oh by the way the length of the numeric part changes. so it can be 1 to n
9:40 am on May 22, 2009 (gmt 0)

Junior Member

5+ Year Member

joined:July 8, 2008
posts:80
votes: 0


sorry for postin again. to mk things easier i'v made the string as follows.

abc999dfg

so i want to know how can i get the string in between the character 'c' and 'd'.

thanks!

10:33 am on May 22, 2009 (gmt 0)

Junior Member

5+ Year Member

joined:July 8, 2008
posts:80
votes: 0


made this ma self. in case anyone needs here it is:

<?php
$string ="a99b";

$start = strpos($string, "a");
$end = strpos($string, "b");
$length = strlen($string);

print "the start is ".$start." end is ".$end." the total length".$length."<br />";

$substr1 = substr($string, $start+1, $length);
print $substr1."<br />";

$new_length = strlen($substr1);
$num = $new_length-1;
//new length is 3

$substr2 = substr($substr1, -$new_length, $num);

print $substr2;
?>

3:36 pm on May 22, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
posts:7999
votes: 0


Glad you got it sorted, but let me ask: are you after only the numeric part of this string?

$string = 'aaa12345vvvv';
$string = preg_replace('/[^\d]+/','',$string);
echo $string;

Basically this regexp says "replace one or more characters not (^) a digit with nothing."

This many have an unexpected result if the strings are not in the format you posted,

$string = 'aaa12345vv6789vv';

will result in

123456789

8:26 am on May 25, 2009 (gmt 0)

Junior Member

5+ Year Member

joined:July 8, 2008
posts:80
votes: 0


in my case i jst want 12345. so thats why i basically tried to get the values between aaa and vv(in ur example). thanks for the post
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members