473,405 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

How do I get the middle of a string?

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
Aug 21 '06 #1
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
Aug 21 '06 #2
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
Aug 21 '06 #3
ronverdonk
4,258 Expert 4TB
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:
Aug 21 '06 #4
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()
Aug 21 '06 #5
ronverdonk
4,258 Expert 4TB
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:
Aug 21 '06 #6
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
Aug 21 '06 #7
ronverdonk
4,258 Expert 4TB
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:
Aug 21 '06 #8
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
Aug 21 '06 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

17
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,...
7
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...
6
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...
1
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 %,...
4
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...
5
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...
4
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...
4
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: ...
5
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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
0
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...
0
marktang
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,...
0
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...
0
jinu1996
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...
0
tracyyun
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...
0
agi2029
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,...
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.