Forum Topic: Finding text with 2 chars (PHP)

(263 views • 7 replies)

This topic is 1 page long.

<< < > >>
Questioning

TitusRevised

Reply To Post Reply & Quote

Posted at: 7/20/06 02:07 PM

TitusRevised LIGHT LEVEL 05

Sign-Up: 02/13/04

Posts: 391

I'd like to find the text in between to find characters in a string.

Example:

$string = "What a nice day this is #its a bad day# to be riding your bicycle!";

I want to withdraw the text inbetween the "#" chars. So it would give me "its a bad day"
or "#its a bad day#"

The text inbetween these chars is unknown in my script so I can't just do strstr(); or something. And if there is no such function, maybe someone could make it?

Thanks in advance.


None

elbekko

Reply To Post Reply & Quote

Posted at: 7/20/06 02:11 PM

elbekko EVIL LEVEL 16

Sign-Up: 07/23/04

Posts: 6,450

<?php
function substr_between($str, $delimiter)
{
$pos1 = strpos($str, $delimiter);
$pos2 = strpos($str, $delimiter, $pos1);
return substr($str, $pos1, $pos2 - $pos1);
}
?>

Something like that should work

"My software never has bugs. It just develops random features. " - Unknown

[ PHP: Main | Omigod, a blog ]

BBS Signature

None

DFox

Reply To Post Reply & Quote

Posted at: 7/20/06 02:11 PM

DFox LIGHT LEVEL 30

Sign-Up: 08/09/03

Posts: 9,270

This should work:

<?php
$string = "What a nice day this is #its a bad day# to be riding your bicycle!";
$parts = explode('#', $string);
echo $parts[1];
?>


None

TitusRevised

Reply To Post Reply & Quote

Posted at: 7/20/06 02:12 PM

TitusRevised LIGHT LEVEL 05

Sign-Up: 02/13/04

Posts: 391

At 7/20/06 02:11 PM, elbekko wrote: <?php
function substr_between($str, $delimiter)
{
$pos1 = strpos($str, $delimiter);
$pos2 = strpos($str, $delimiter, $pos1);
return substr($str, $pos1, $pos2 - $pos1);
}
?>

Something like that should work

;
D Thank you very much!


None

TitusRevised

Reply To Post Reply & Quote

Posted at: 7/20/06 02:24 PM

TitusRevised LIGHT LEVEL 05

Sign-Up: 02/13/04

Posts: 391

At 7/20/06 02:11 PM, elbekko wrote: <?php
function substr_between($str, $delimiter)
{
$pos1 = strpos($str, $delimiter);
$pos2 = strpos($str, $delimiter, $pos1);
return substr($str, $pos1, $pos2 - $pos1);
}
?>

Something like that should work

Wait will this find all instances of text in between the delimiter?
Example: "This is a nice day #this is a bad day# to be riding your bike. Even though #it's getting dark# I know it's getting late."
I want to find both of those. What I'm basically trying to do is delete any text in between the two chars I want. So in my string, any text in between the char I want will be deleted. All instances. It will have multiple places of "#texthere# blah blah #text here#"
Thanks again.


None

elbekko

Reply To Post Reply & Quote

Posted at: 7/20/06 02:25 PM

elbekko EVIL LEVEL 16

Sign-Up: 07/23/04

Posts: 6,450

Will only get the first instance.

"My software never has bugs. It just develops random features. " - Unknown

[ PHP: Main | Omigod, a blog ]

BBS Signature

None

DFox

Reply To Post Reply & Quote

Posted at: 7/20/06 02:28 PM

DFox LIGHT LEVEL 30

Sign-Up: 08/09/03

Posts: 9,270

At 7/20/06 02:24 PM, TitusRevised wrote: I want to find both of those. What I'm basically trying to do is delete any text in between the two chars I want. So in my string, any text in between the char I want will be deleted. All instances. It will have multiple places of "#texthere# blah blah #text here#"
Thanks again.

Use what I gave you :)


None

elbekko

Reply To Post Reply & Quote

Posted at: 7/20/06 02:46 PM

elbekko EVIL LEVEL 16

Sign-Up: 07/23/04

Posts: 6,450

Yeah, you could do something like this:

function subst_between_all($str, $delimiter)
{
$parts = explode('#', $string);
for($i = 1; $i <= count($parts); $i += 2)
{
$matches[] = $parts[$i];
}
return $matches;
}

"My software never has bugs. It just develops random features. " - Unknown

[ PHP: Main | Omigod, a blog ]

BBS Signature

All times are Eastern Standard Time (GMT -5) | Current Time: 08:33 AM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!