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
https://forums.phpfreaks.com/topic/106782-solved-string-manipulation/
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];
?>

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>

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

<?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;
}
?>

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?

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++;
}
?>

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

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.