cahamilton Posted May 24, 2009 Share Posted May 24, 2009 Hello again....quick question! How would I rewrite a variable, so that everything after a certain point in the text, is deleted? Currently, Im trying to do something like this: The Offspring – Long Way Home <<< Starting with this The Offspring <<< End like this So anything after the " – " I want to delete. I think it has something to do with preg_replace, but I cant quite seem to get my head around the actual function itself to rewrite it. Would anyone be able to explain it to me? Quote Link to comment https://forums.phpfreaks.com/topic/159432-rewriting-a-variable/ Share on other sites More sharing options...
.josh Posted May 24, 2009 Share Posted May 24, 2009 would be faster and less complicated to just explode at the hyphen but if you really want to do it with preg_replace it would be $string = preg_replace('~-.*~','',$string); Quote Link to comment https://forums.phpfreaks.com/topic/159432-rewriting-a-variable/#findComment-841014 Share on other sites More sharing options...
hitman6003 Posted May 24, 2009 Share Posted May 24, 2009 Or use a combo of substr and strpos... echo substr($string, 0, strpos("-", $string)); Quote Link to comment https://forums.phpfreaks.com/topic/159432-rewriting-a-variable/#findComment-841015 Share on other sites More sharing options...
cahamilton Posted May 24, 2009 Author Share Posted May 24, 2009 Well anyway of doing it would be great. Would there be any advantages using explode() over the preg_replace? Quote Link to comment https://forums.phpfreaks.com/topic/159432-rewriting-a-variable/#findComment-841018 Share on other sites More sharing options...
.josh Posted May 24, 2009 Share Posted May 24, 2009 it's (negligibly) faster and easier for most people to grasp (most people run away screaming at any mention of regex) Quote Link to comment https://forums.phpfreaks.com/topic/159432-rewriting-a-variable/#findComment-841024 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.