943,767 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 685
  • PHP RSS
Sep 11th, 2009
0

Find random string between main string?

Expand Post »
Hi,

I want to find random string between main string.

Suppose
$string="Sign up for Facebook to connect with Jean Profession";

I want do that ..

random word who having length greater than 4 and word not equal to Facebook.


How to do this??
Reputation Points: 3
Solved Threads: 15
Posting Whiz
Aamit is offline Offline
341 posts
since Apr 2008
Sep 11th, 2009
1

Re: Find random string between main string?

Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005
Sep 11th, 2009
0

Re: Find random string between main string?

hi,

my string is
$string="Sign up for Facebook to connect with Jean Profession";

i want to find word randomly from string

like :- connect or Profession whose length >4 and this word not is a facebook.
Reputation Points: 3
Solved Threads: 15
Posting Whiz
Aamit is offline Offline
341 posts
since Apr 2008
Sep 11th, 2009
0

Re: Find random string between main string?

Click to Expand / Collapse  Quote originally posted by Aamit ...
hi,

my string is
$string="Sign up for Facebook to connect with Jean Profession";

i want to find word randomly from string

like :- connect or Profession whose length >4 and this word not is a facebook.
Yes, you described that, which is why I posted a link describing how to do it.
Last edited by ShawnCplus; Sep 11th, 2009 at 6:01 pm.
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005
Sep 11th, 2009
0

Re: Find random string between main string?

Click to Expand / Collapse  Quote originally posted by Aamit ...
hi,

my string is
$string="Sign up for Facebook to connect with Jean Profession";

i want to find word randomly from string

like :- connect or Profession whose length >4 and this word not is a facebook.
You can use regular expressions or string functions.

String functions: http://us2.php.net/manual/en/book.strings.php
Probably the simplest way to go about it is to explode() the string into an array of words.

eg:

PHP Syntax (Toggle Plain Text)
  1. $array = explode(" ", $string);

Then loop each array values (each word) counting characters with strlen().

For an intro to regular expressions see: http://www.regular-expressions.info/

It also goes into it in depth after a while. Regex is a really useful tool to learn, and is implemented similarly across programming languages. The flavor PHP currently uses is PCRE. http://www.pcre.org/

The PHP docs: http://www.php.net/pcre
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005
Sep 11th, 2009
0

Re: Find random string between main string?

Try this:
php Syntax (Toggle Plain Text)
  1. <?
  2. $string='Sign up for Facebook to connect with Jean Profession';
  3. $word=explode(' ',$string);
  4. $b=(count($word)-1);
  5. $b=mt_rand (0,$b);
  6. echo $word[$b];
  7. ?>
Sponsor
Featured Poster
Reputation Points: 410
Solved Threads: 258
Occupation: Genius
cwarn23 is offline Offline
3,004 posts
since Sep 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Trouble with is_null()
Next Thread in PHP Forum Timeline: Adding up numbers in array





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC