I have a question. If I have a string like this...
$a = "I don't need this @This is the middle I need # This is the end I don't need";
Does anyone know the command that would let me get "This is the middle I need" out of this string and put it into $b?
Any help would be appreciated.
Thanks
Don
8 4509
why would you need to pull the centre out of this line, if you set up your html/php code correctly it could be on its own therefore no need to pull it out of the $a
I have a database of full names such as "Michael Ian Black". I want to extract "Ian" from this string. Bascially I want to know the commands to strip "Michael " from this string by searching for the first space. Then have something like this.
$a = "Michael Ian Black";
$b = "Ian Black";
Then I want to search for the first space again and strip everything after it.
$b = "Ian Black";
$c = "Ian";
Does anyone know the commands for this or can write a simple code to show me how to do this?
Thanks
Don
If you always know what your separators are, the following function will do.
[PHP]
<?php
$a = "I don't need this @This is the middle I need # This is the end I don't need";
if ($b = extractit($a, '@', '#'))
echo $b;
else
echo 'Not found';
// the above will return: This is the middle I need
/////////////////////////////////////////////////////////////
// specify: string, start separator, close separator
function extractit($string, $s, $e) {
if ($start=strpos($string, $s)) {
if ($end=strpos(substr($string,$start+1), $e)) {
return substr($string, $start+1, $end-1);
}
else {
return false;
}
}
return false;
}
?>
[/PHP]
Ronald :cool:
Ronald,
I get an error when I use that command. Any other ideas or do you know why I would get that error?
Fatal error: Call to undefined function: extractit()
You must have copied/pasted something wrong from the forum entry. It is a normal PHP function. Reworked outside a function it is as follows:
[PHP]<?php
$a = "I don't need this @This is the middle I need # This is the end I don't need";
if ($start=strpos($a, '@')) {
if ($end=strpos(substr($a,$start+1), '#')) {
$b = substr($a, $start+1, $end-1);
echo $b;
}
else {
echo 'end-separator not found';
}
}
else
echo 'start-separator not found';
?>[/PHP]
Ronald :cool:
Ronald,
You are right. I must of copied something wrong. I just tried it and it worked. Thank you very much. You saved me hours of trying to figure this out.
Dean
A last word on this: I asume you use the @ and # just to be able to distinguish between the first and last separator. You could also use just one standard separator, like @.
That way you can use the PHP explode() function. And then the code would be alot easier, such as:
[PHP]<?php
$a = "I don't need this @This is the middle I need.@ This is the end I don't need";
$parts = explode("@", $a)
echo $parts[0] // results in: I don't need this
echo $parts[1] // results in: This is the middle I need.
echo $parts[2] // results in: This is the end I don't need
?>[/PHP]
Keep cool!
Ronald :cool:
Actually I needed 2 separate separators in this case. But I may be able to use your other example in the future.
Thanks again
Dean
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Olivier Bellemare |
last post by:
I've tried to make a function that returns the middle of a string. For
example:
strmid("this is a text",6,4); would return "is a".
Here is my code:
char *strmid(char *texte, int depart,...
|
by: tano |
last post by:
Hello,
I have to insert a char in the middle of a string, I have written two functions
but I don't know what is the better? The problem is: if I use malloc() I copy
all the string with the new...
|
by: Ian Williamson |
last post by:
Greetings,
My company has an ASP.NET based enterprise product that is undergoing some
changes and I need some community input to help solve a problem.
In the current implementation, any given...
|
by: Anandan |
last post by:
Hi,
This is regarding Dataset Filter:
WILDCARD CHARACTERS
Both the * and % can be used interchangeably for wildcards in a LIKE
comparison. If the string in a LIKE clause contains a * or %,...
|
by: Chris |
last post by:
Hi Everyone,
I am using a regex to check for a string. When all the file contains is my
test string the regex returns a match, but when I embed the test string in
the middle of a text file a...
|
by: mishink7 |
last post by:
i was wondering how would u remove multiple whitespaces from the middle of a string to make it only one white space...for example:
string hi= "hi bye";
then remove the multiple whitespaces...
|
by: mdthom08 |
last post by:
I need to return the middle 2 characters of a word, favoring the right.
so for example:
SevenMethods myFuns = new SevenMethods();
assertEquals("34", myFuns.middleTwo("12345"));
so far i...
|
by: Ben |
last post by:
Hello All:
I am new to Python, and I love it!! I am running 2.6 on Windows. I
have a flat text file here is an example of 3 lines with numbers
changed for security:
...
|
by: rtillmore |
last post by:
Hi,
I have run into a small snag with a program I am writing. I am
generating a string using openssl DES_ecb_encrypt. The string is
properly generated and contains the correct value. The...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |