PHP String Replace

I am looking to add my date to the beginning of my articles.
Previously, I
was using the string replace function to look for the
begininng of my
paragraph and then place the date in front of that. Bad Jon!
I forgot that
any article with multiple paragraphs would then have multiple
dates.
$artContent = str_replace('<p>', '<p>
'.$artDateEdited.' - ',
$artContent);
So, what I am looking for is how do I do a string replaceon
just the first
paragraph?
TIA,
Jon Parkhurst
PriivaWeb
http://priiva.net.

Are you sure there are <p> tags in the data? The only
way that could get
there would be if someone explicitly put them there.
Murray --- ICQ 71997575
Adobe Community Expert
(If you *MUST* email me, don't LAUGH when you do so!)
==================
http://www.dreamweavermx-templates.com
- Template Triage!
http://www.projectseven.com/go
- DW FAQs, Tutorials & Resources
http://www.dwfaq.com - DW FAQs,
Tutorials & Resources
http://www.macromedia.com/support/search/
- Macromedia (MM) Technotes
==================
"crash" <[email protected]> wrote in message
news:[email protected]...
> LOL, just now getting back to this.
>
> I've got myself in a little bit of a bind, b/c I didn't
foresee my data
> having more than one paragraph.
>
> This is what I'm thinking of doing. If there's a better
way, can somebody
> pipe in?
>
> Take data from database.
> Search for <p> tags, and capture all text within
the first instances
> Limit text to X number of chars
> Replace <p> tags, and my ...Full Story links
>
> Looking now through all of the string functions and
preg_replace right
> now.
>
> Basically, I"m having some problems finding a function
that will replace
> $var on the Xth instance.
>
> Will post tomorrow with my results.
> "Joe Makowiec" <[email protected]> wrote in
message
>
news:[email protected]...
>> On 26 Aug 2006 in macromedia.dreamweaver.appdev,
crash wrote:
>>
>>> I am looking to add my date to the beginning of
my articles.
>>> Previously, I was using the string replace
function to look for the
>>> begininng of my paragraph and then place the
date in front of that.
>>> Bad Jon! I forgot that any article with multiple
paragraphs would
>>> then have multiple dates.
>>>
>>> $artContent = str_replace('<p>',
'<p>
>>> '.$artDateEdited.' - ',
>>> $artContent);
>>>
>>> So, what I am looking for is how do I do a
string replaceon just the
>>> first paragraph?
>>
>>
http://www.php.net/manual/en/function.preg-replace.php
>>
>> In particular, the optional 4th parameter to
preg_replace() is a limit;
>> you can set this to 1.
>>
>> --
>> Joe Makowiec
>>
http://makowiec.net/
>> Email:
http://makowiec.net/email.php
>
>

Similar Messages

  • Basic php string operations

    Example
    <?php
    $url = '
    http://username:password@hostname/path?arg=value#anchor';
    print_r(parse_url($url));
    echo parse_url($url, PHP_URL_PATH);
    ?>
    output:
    Array
    [scheme] => http
    [host] => hostname
    [user] => username
    [pass] => password
    [path] => /path
    [query] => arg=value
    [fragment] => anchor
    /path
    Question:
    For example, I want to do something with the host, like run
    some string replace operations on it, but how do I access
    it?

    .oO(ttdevelop)
    >Example
    > <?php
    > $url = '
    http://username:password@hostname/path?arg=value#anchor';
    >
    > print_r(parse_url($url));
    >
    > echo parse_url($url, PHP_URL_PATH);
    > ?>
    >
    > output:
    >
    > Array
    > (
    > [scheme] => http
    > [host] => hostname
    > [user] => username
    > [pass] => password
    > [path] => /path
    > [query] => arg=value
    > [fragment] => anchor
    > )
    > /path
    >
    > Question:
    > For example, I want to do something with the host, like
    run some string
    >replace operations on it, but how do I access it?
    You should read about how to use variables and arrays or use
    the second
    parameter of parse_url() to only return the host name as a
    string like
    you did in the second example (see the manual for details).
    Then assign
    the return value to a variable and you can do whatever you
    want with it.
    Variables
    http://www.php.net/manual/en/language.variables.php
    Arrays
    http://www.php.net/manual/en/language.types.array.php
    parse_url
    http://www.php.net/manual/en/function.parse-url.php
    Micha

  • String replacement

    Hi, anyone knows how to replace a particular string in a file with another string?
    For example, in the file hello.tmp,
    I want to replace the word :Smile: with the string
    <img src="smile.gif">
    I tried to use String.replace() but this is only for characters.
    Anyone knows how to replace Strings??
    Hope to hear from anyone really soon. Very urgent!!!

    There is no direct method to replace a string in file. The code goes like this:
    import java.io.*;
    import java.util.*;
    public class StringReplacer
         private String replaceString;
         private String replacerString;
         private String filePath;
         public StringReplacer(String replaceString, String replacerString, String filePath)
              this.replaceString = replaceString;
              this.replacerString = replacerString;
              this.filePath = filePath;
              try
                   replace();               
              catch (Exception ex)
         private void replace() throws Exception
              File file = new File(filePath);
              if(!file.exists())
                   System.out.println("File doesnot exists");
                   return;
              DataInputStream dis = new DataInputStream(new FileInputStream(file));
              StringBuffer fileData = new StringBuffer("");
              String strTemp = "";
              while((strTemp = dis.readLine()) != null)
                   fileData.append(strTemp);
              dis.close();
              StringTokenizer stTemp = new StringTokenizer(fileData.toString(), replaceString);
              int tokens = stTemp.countTokens();
              if(tokens <= 1)
                   //No matches found for string to be replaces.
                   System.out.println("No matches found for string to be replaced");
                   return;
              fileData = new StringBuffer("");
              while(stTemp.hasMoreTokens())
                   fileData.append(stTemp.nextToken() + replacerString);     
              if(file.exists())
                   file.delete();
              file = new File(filePath);
              if(!file.exists())
                   file.createNewFile();
              DataOutputStream dos = new DataOutputStream(new FileOutputStream(file));
              dos.writeChars(fileData.toString());
              dos.flush();
              dos.close();
         public static void main(String [] args)
              //create an object of StringReplacer here.
    }

  • What is the unicode for ''  (need it to do String.replace)

    i'm trying to use String.replace(char a, char b)
    it won't compile if i do x.replace('_', '')
    help!
    thanks!

    Are you trying to remove all occurences of the underscore character from the string? If so create a StringBuffer from the string, loop through it and remove the characters as you find them.
    If you're using JDK1.4 you should eb able to use the new String method:
    public String replaceAll(String regex,
    String replacement)

  • String replace

    String has a replace method for chars, something the likes of
    String replace(char a, char b)
    I tend to think it would be nice to ammend String further with
    String replace(String a, String b)
    proponents/opponents? It seems like a nice thing to have.

    This type of method is available in the regex package
    (available in JDK 1.4). I am not expert or regular
    expressions but it will allow you to do this and much
    much more.I guess it will. Still it would be nice to have the ability directly inside String without explicitly having to instantiate a Pattern. I know there is a split method in String in JSDK 1.4 which takes a String specification of a regexp and internally compiles it. I guess my point is convenience but I should know the new APIs fully before writing more.

  • Regarding String Replacing ....

    Hi Friends,
    I have some problem regarding String replacing.
    ex.
    I have following string ::
    =================================================
    This is the java manager the main purpose for the jasper is used to create reports.
    And the Java language is very extensible.
    Is this the proper string functions provided by java isn't it ?
    *================================================*
    *i want to replace word "is"  with "IS". but that word is not connected to word like  Th{color:#ff00ff}is{color}*
    *differ from simple separate "{color:#ff0000}is{color}" so Word should not be replaced in This only separate "is"*
    *must be replaced. ?*
    *Any Idea ?*
    *{color}*
    Edited by: Ghanshyam on Sep 14, 2007 4:27 PM

    How about just searching for and replacing
    " is "
    instead of
    "is"
    So, using this code from the Java Developers Almanac 1.4 (2002 Addison-Wesley):-
    static String replace(String str, String pattern, String replace) {>    int s = 0;>    int e = 0;>    StringBuffer result = new StringBuffer();> >    while ((e = str.indexOf(pattern, s)) >= 0) {>       result.append(str.substring(s, e));>       result.append(replace);>       s = e+pattern.length();>  result.append(str.substring(s));
    return result.toString();
    }Your would use:-
    String newStr = replace(originalStr, " is "," IS ");
    Edited by: mad_scientist on Sep 14, 2007 4:16 AM (Sorry about all the edits, just getting used to the formatting on here)
    Edited by: mad_scientist on Sep 14, 2007 4:22 AM
    Edited by: mad_scientist on Sep 14, 2007 4:24 AM

  • PLD String Replace Function?

    Hello Experts,
    I would like to use the Sum in Words option in the field properties Format tab. However, I would like to have this in a language for which there is no LRF.
    I have read all the various Sum in Words/Amount in Words threads I can find but I have no special locale based requirements (lacs etc). The default answer seems to be to prepare a FMS query and attach it to a UDF, then pull the variable into PLD.
    Rather than rewriting the functionality, it would be far easier for me to use a series of Replace functions (the structure of Turkish for amount in words is exactly the same as English and translating each word (One, Two... Ten, Twenty... etc) with a Replace would solve my problem.
    Looking at the documentation for PLD, I can see no prebuilt Replace function - is there any way this can be achieved?
    If not, is there any Sum in Words function in B1 queries so I can at least do the replace there rather than writing an entire convert to words query?
    Thank you.

    Wasn't able to force a string replace - had to hex edit the LRF instead. Quick and dirty, but it works.

  • Batch Rename with String Replacement?

    Is there a convenient way to batch rename files after they've been imported? The template mechanism seems to work well for creating new names, but not for renaming. For example, I originally imported and renamed my images to look like this:
    Zemke_YYMMDD_NNN-O1.CR2
    I decided I would rather have DRZ (My initials) instead of "Zemke" as the prefix. I was able to get it done with some contortions:
    Metadata/XMP/Export
    Remove all files from Lightroom
    Rename all the external files to start with DRZ
    Reimport all the files
    Delete the XMP files
    I think what is needed is for Rename to include string replacement (and string deletion) functionality. Is it already there?
    Dan

    Stefano, nonadjacent selection can be done by holding down Control and manually selecting the additional image. If you want to select a lot of separate ranges, the way to do it is to add each range to the quick collection (press the B key), then go to the quick collection and select all. It's exactly for those kinds of scenarios that quick collection exists. Once you get used to doing it, it's easier working with a "sticky" selection like the quick collection rather than the contortions required to extend standard selections.
    To slightly add to Rory's comments: two-digit year plus spelled out month names and day of the week names are also available. Combine into any order you want, with any additional supporting text anywhere you want it, and you've got huge flexibility there. And no matter how complex it is, you can save it as a preset so it's readily accessible later.

  • Whole word string replacement in LV7.1

    Is there anyway to do whole word string replacement in 7.1?
    For example, if I have the string "x21+sin(x2)" and I want to replace "x2" with "b2" without accidently changing "x21" into "b21". I saw that 7.1 does not support the word boundary "\b" in regex.

    The easy solution is to search for "(x2)" and replace with "(b2)" but if you're not sure that x2 will always be in parenthesis, then another solution would be to get the next character after "x2" is found, and if it is not a number (0-9) then you have found x2, if it is a number then you have found x21, or x22, etc.
    Unofficial Forum Rules and Guidelines - Hooovahh - LabVIEW Overlord
    If 10 out of 10 experts in any field say something is bad, you should probably take their opinion seriously.

  • Help needed with String replace!

    Hi,
    I would like to replace all '\' characters with '/' in my string, ie:
         mystring.replace('\','/');
    Howeve, it complain about the '/' (Unclosed character literal).
    Any suggestions?
    Thanks

    Show us the line, you are probably using your regex incorrectly.
    In order to get the '\' you have to escape it (the '\' char. is used as the escape character so that you can look for things like " ' and new lines by useing \" \' and \n. Otherwise, how would java know you are looking for quotes and not closing your string?) As a consequence, your regex should look like '\\" for the \.
    The replace statement should probably be:
    string.replace("\\", "/");

  • String Replace  has $

    Hi All,
    How do i replace the text with text that has $.
    String s = "Sample text XYZ";
    s.replaceAll(XYZ, "$ABC");
    Error: Illegal Group Reference.
    "\\$ABC" works.
    In my case "$ABC" is dynamic text. Position of $ can be any where in in the text, need not be first char.
    TIA,
    Kishore.

    I think you'd need to find the $ in your search string first and put the escape character in their before attempting to do the replace all. In case you're unaware, RegEx uses $ as a special character, so that's why it must be replaced..
    String a = "This is XYZ";
    String replace = "$ABC";
    StringBuffer sb = new StringBuffer(replace);
    sb.insert(sb.indexOf("$"), "\\ ");
    a = a.replaceAll("XYZ", sb.toString());
    System.out.println(a);(Note: The extra space after the \\ in the insert was just so this forum would display everything properly).

  • PHP: automatically replace alphanumeric characters in a string

    Quick question:
    How do I program a PHP script to replace all non-alphanumeric
    charters within a string, with underscores?

    .oO(AngryCloud)
    >Yes, this does help, although it is only alphabetic.
    >
    >Will changing the line to this make it alphanumeric?:
    >
    >$new_string = preg_replace("/[^a-zA-Z0-9]/", "_",
    $string);
    Did you try it? ;-)
    You could also use this shorter pattern:
    /[^a-z\d]/i
    Should be the same (\d matches decimals and the /i modifier
    makes the
    entire thing case-insensitive).
    Micha

  • Php / mysql  replace non-ascii character in a string

    I have a script that converts a ms word document to text then
    uploads that to a blob field on a mysql db.
    During the conversion some characters my not be recognised.
    When i then call up the blob for display on the browser...those
    characters show up as unknown characters with a ? or box. Is there
    a way to preg_replace those unknown characters before displaying
    them.
    thanks
    ian

    .oO(surfinIan)
    >I have a script that converts a ms word document to text
    then uploads that to a
    >blob field on a mysql db.
    > During the conversion some characters my not be
    recognised. When i then call
    >up the blob for display on the browser...those characters
    show up as unknown
    >characters with a ? or box. Is there a way to
    preg_replace those unknown
    >characters before displaying them.
    What about fixing the encoding problem instead? If chars get
    lost during
    such a transfer
    document->script->database->script->browser it's always
    an encoding problem somewhere down the road.
    The recommendation these days is to use UTF-8, which avoids
    most of
    these old problems. You just have to make sure that your
    documents are
    properly stored as UTF-8 in the database and delivered as
    such to the
    script and the browser, then you don't have to worry about
    special chars
    anymore.
    That's just the general idea. I can't be more specific, since
    I don't
    know your conversion script or the database structure.
    Micha

  • Very simple XSLT string replacing question

    Hi,
    This is a really simple question for you guys, but it took me long, and i still couldn't solve it.
    I just want to remove all spaces from a node inside an XML file.
    XML:
    <?xml version="1.0" encoding="UTF-8"?>
    <root insertedtime="2008-05-01T14:03:00.000+10:00" pkid="23421">
       <books>
          <book type='fiction'>
             <author>John Smith</author>
             <title>Book title</title>
             <year>2 0  0 8</year>
          </book>
       </books>
    </root>in the 'year' node, the value should not contain any spaces, that's the reason why i need to remove spaces using XSLT. Apart from removing space, i also need to make sure that the 'year' node has a non-empty value. Here's the XSLT:
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
        <xsl:strip-space elements="*"/>
        <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>
        <xsl:template match="//books/book[@type='fiction']">
            <xsl:copy>
                <xsl:apply-templates select="@*"/>
                <xsl:attribute name="id">101</xsl:attribute>
                <xsl:call-template name="emptyCheck">
                    <xsl:with-param name="val" select="year"/>
                    <xsl:with-param name="type" select="@type"/>
                    <xsl:with-param name="copy" select="'true'"/>
                </xsl:call-template>
                <xsl:value-of select="translate(year, ' ', '')"/>
            </xsl:copy>
        </xsl:template>
        <!-- emptyCheck checks if a string is an empty string -->
        <xsl:template name="emptyCheck">
            <xsl:param name="val"/>
            <xsl:param name="type"/>
            <xsl:param name="copy"/>
            <xsl:if test="boolean($copy)">
                <xsl:apply-templates select="node()"/>
            </xsl:if>
            <xsl:if test="string-length($val) = 0 ">
                <exception description="Type {$type} value cannot be empty"/>
            </xsl:if>
        </xsl:template>
    </xsl:stylesheet>The 'emptyCheck' function works fine, but the space replacing is not working, this is the result after the transform:
    <?xml version="1.0" encoding="utf-8"?>
    <root insertedtime="2008-05-01T14:03:00.000+10:00" pkid="23421">
       <books>
          <book type="fiction" id="101">
             <author>John Smith</author>
             <title>Book title</title>
             <year>2 0 0 8</year>2008</book>
       </books>
    </root>The spaced year value is still there, the no-space year is added outside the 'year' tags'
    anyone can help me, your help is extremely appreciated!
    Thanks!

    You should add a template for 'year' :<xsl:template match="year">
    <year><xsl:value-of select="translate(.,' ','')"/></year>
    </xsl:template>and remove the translate call in the 'book' template.
    It would be better to add a 'priority' attribute at each template so it would be explicit which template to use because match="@*|node()" could be interpreted by another transform engine as the unique template to be used !

  • How to do a string replacement with a special characters?

    Hi there, I've a string which I need to do a replacement in but replaced and/or replacement contain a special chars.
    I.e. like this:
    newConvertedString = newConvertedString.replaceAll("</soapenv:Body>", "");
    newConvertedString = newConvertedString.replaceAll("<default:edit-config>", "</default:edit-config>");
    It does not do anything. I know that repalceAll(regexc., regexp.) but what can I use instead??
    Thanks ina advance

    qavlad wrote:
    Thanks 'jverd'. Just one more question - do you know if there any limit on the replacement string/regex used with/in .replaceAll()??No, the only limit would be the limit on how big a string can be and how much memory you have. However, anpoorly written regex on a long String will take a long time to execute.
    Seems like when I'm trying to replace one like this (and even a longer ones) it does not replace anything while it's there.
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Header><wsse:Security xmlns:wsss="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wssa="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
    Then there was no match.

Maybe you are looking for

  • Battery power for ibook g4

    so as many of you ibook users know, all batteries die eventually. i have an early 2004 ibook and i get about 40 minutes of battery power. i am going to purchase a new battery from apple. i would like to know how i can get the MOST POSSIBLE LIFE out o

  • Related to Basket redirction

    Hi Explain how the basket will progress into checkout for both logged-in and not logged-in users. Could you please tell me  we need to do an any customization for  redirecting  user to payment page or login on page when user click  checkout button(in

  • Order Status Picked and Picked Partial

    Hi, I am worknig on OM. I need to get the orders details where the order status in Picked and Picked Partial. I checked in oe_order_lines_all, the flow status code is not holding picked and picked partial. Please any one let me know how to get Picked

  • Problem in SunStudio12 installation!!!

    hi, i've installed netbeans6 and as i develop c++ application, i need a appropriate compiler. i download SunStudio12ml-linux-x86-200709-pkg.tar.bz2. these are the following steps i made : 1- tar xjvf SunStudio12ml-linux-x86-200709-pkg.tar.bz2 2 - ./p

  • Very similar FLWOR queries with very dissimilar performances

    Hello, I have two queries which seem to me to be equivalent but have very different performances (<1s vs >30s) Sample xml data <MARQUE> <CodeApp>080000000</CodeApp> </Wares>blah blah blah</Wares> </MARQUE> Query #1, with acceptable performances query