Find random string between main string?

Reply

Join Date: Apr 2008
Posts: 293
Reputation: Aamit has a little shameless behaviour in the past 
Solved Threads: 11
Aamit Aamit is offline Offline
Posting Whiz in Training

Find random string between main string?

 
0
  #1
Sep 11th, 2009
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??
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,419
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 229
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey

Re: Find random string between main string?

 
1
  #2
Sep 11th, 2009
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 293
Reputation: Aamit has a little shameless behaviour in the past 
Solved Threads: 11
Aamit Aamit is offline Offline
Posting Whiz in Training

Re: Find random string between main string?

 
0
  #3
Sep 11th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,419
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 229
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey

Re: Find random string between main string?

 
0
  #4
Sep 11th, 2009
Originally Posted by Aamit View Post
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.
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,082
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: Find random string between main string?

 
0
  #5
Sep 11th, 2009
Originally Posted by Aamit View Post
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:

  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
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,518
Reputation: cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about 
Solved Threads: 136
cwarn23's Avatar
cwarn23 cwarn23 is offline Offline
Posting Virtuoso

Re: Find random string between main string?

 
0
  #6
Sep 11th, 2009
Try this:
  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. ?>
Try not to bump 10 year old threads as it can be really annoying.
http://syntax.cwarn23.net/
Smilies: ^_* +_+ v_v -_- *~*`
My favourite PC. - MacGyver Fan
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC