Jump to content

[SOLVED] String manipulation


Dr-Neo

Recommended Posts

Hello All,  :)

I have a problem with String manipulation :

$string = "Hello And welcome To ^#phpfreaks# the best php forum";

how to echo the phpfreak word only?

i mean how to extract it from the variable?

i need to know how to extract words between 2 symbols even the symbols is *&^%$?

thank you

 

Link to comment
Share on other sites

Lot's of ways...off the top of my head, here are two:

<?php
  $string = "Hello And welcome To ^#phpfreaks# the best php forum";
  $s = strpos($string,'#');
  print substr($string,$s+1,strpos($string,'#',$s+1) - $s - 1);
?>

<?php
  $string = "Hello And welcome To ^#phpfreaks# the best php forum";
  if(preg_match('/#(.+?)#/',$string,$matches))
    print $matches[1];
?>

Link to comment
Share on other sites

This may work, depending on your entire data set:

 

<pre>
<?php
$data = <<<DATA
	Hello And welcome To ^#phpfreaks# the best php forum
	Hello And welcome To ^#phpfreaks& the best php forum
	Hello And welcome To %#phpfreaks# the best php forum
	Hello And welcome To phpfreaks$ the best php forum
	Hello And welcome To ^#phpfreaks# the best php forum
	Hello And welcome To ^^phpfreaks^ the best php forum
DATA;

preg_match_all('/[\p{S}\p{P}]+(.+?)[\p{S}\p{P}]+/', $data, $matches);
print_r($matches);
?>
</pre>

Link to comment
Share on other sites

thank you so much all

the closet solution for me is

<?php
  $string = "Hello And welcome To ^#phpfreaks# the best php forum";
  $s = strpos($string,'#');
  print substr($string,$s+1,strpos($string,'#',$s+1) - $s - 1);
?>

but the problem that its make it for once.

if the text like that

$string = "Hello And welcome To ^#phpfreaks# the best php forum

and the ^#yahoo# is the best email and my ^#facebook# is the best site";

it will only print phpfreaks and i want print all of the results

could you please help me to loop through it

thank you so much

Link to comment
Share on other sites

<?php
$string = "Hello And welcome To ^#phpfreaks# the best php forum and the ^#yahoo# is the best email and my ^#facebook# is the best site";
if(preg_match_all('/\^#(.+?)#/',$string,$matches)){
  foreach($matches[1] as $match)
    print $match;
}
?>

Link to comment
Share on other sites

thank you so much for your post.

<?php
$string = "Hello And welcome To ^#phpfreaks# the best php forum and the ^#yahoo# is the best email and my ^#facebook# is the best site";
if(preg_match_all('/\^#(.+?)#/',$string,$matches)){
  foreach($matches[1] as $match)
    print $match;
}
?>

this code posted before by effigy and it give me a lot of errors when i tried to change the symbols

so i used this code

<?php
  $string = "Hello And welcome To ^#phpfreaks# the best php forum";
  $s = strpos($string,'#');
  print substr($string,$s+1,strpos($string,'#',$s+1) - $s - 1);
?>

and its working just like dream.

the problem when i need use more than one time

is there anyway to use the same code for than one time in the same variable?

Link to comment
Share on other sites

Here you go

 

<?php
$string = "Hello And welcome To ^#phpfreaks# the best php forum and the ^#yahoo# is the best email and my ^#facebook# is the best site";

$pattern = '/#(.*?)#/';                  
preg_match_all($pattern,$string,$data,PREG_SET_ORDER);

$a = 0;
$count = count($data);

while($a < $count) {
  echo $data[$a][1].'<br />';
  $a++;
}
?>

Link to comment
Share on other sites

Thank you so much all.

and thank you minidak03.

<?php
$string = "Hello And welcome To ^#phpfreaks# the best php forum and the ^#yahoo# is the best email and my ^#facebook# is the best site";

$pattern = '/#(.*?)#/';                  
preg_match_all($pattern,$string,$data,PREG_SET_ORDER);

$a = 0;
$count = count($data);

while($a < $count) {
  echo $data[$a][1].'<br />';
  $a++;
}
?>

this is the one  ;) problem resolved

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.