www.webdeveloper.com
Results 1 to 15 of 22

Thread: This preg_replace how do I use it with arrays ?

Threaded View

  1. #1
    Join Date
    May 2006
    Posts
    246

    This preg_replace how do I use it with arrays ?

    Hi,

    I have some data to find and replace from a string.

    The find and replace data is in a mysql table
    so I can easily extract it and build 2 arrays,
    or build an associated array

    ( I am not sure which, so I did both)

    PHP Code:
    $sql "SELECT word,replacement FROM theos order by word";
        
    $result mysql_query($sql) or die("Could NOT select words"mysql_error() );

        
    $words = array();
        
    $replacements= array();
        
    $words_reps = array();
        
    $new_words = array();
        
    $new_words_reps = array();
        
        while (
    $row mysql_fetch_assoc($result)) {
            
    extract($row);
            
    $words[] = $word;
            
    $replacements[] = $replacement.' ';
            
    $words_reps[$word] = $replacement ;
            } 
    The string to do the find and replace on is a sentence like this:

    $input_data =
    there is something about being the parent of a very small child a child who has not yet begun to form words that has exerted a subtle pressure on the way i think about language in general my investment in words is heavy and more or less literal in the sense that they are the means by which i make at least in theory my living since my son was born six months ago the first way in which my relationship with words has changed is this i haven't been able to get nearly enough of the words down on paper

    Now I could make that array:
    PHP Code:
    $input_data_arr explode(' '$input_data); 
    But I don't know if I need to.

    My FIND array $words
    looks like this:


    2
    a
    about
    access to
    across the
    advert
    advice
    ago
    agreement

    My REPLACE array $replacements
    looks like this:

    {2|two|only two}
    {a|some|a very|a definite|a specific|a particluar}
    {about|regarding|concerning|with regards to|in relation to|related to|pertaining to|on the subject of|when it comes to|in regards to|in regard to|with regard to|on the subject off|as regards to}
    {access to|usage of|use of|the means to access}
    {across the|around the|all over the|over the|through the|round the|all around the}
    {advert|advertisement|ad|offer|promotion|special offer|promo|campaign|deal|special promotion|special deal}
    {advice|guidance|suggestions|tips|assistance|information|help and advice|recommendations|help|aid|instruction|counsel}
    {ago|back|in the past|earlier|previously|previous|prior}
    {agreement|arrangement|contract|deal}


    AND THE associated array:

    ( Using this to list it:

    PHP Code:
    foreach($words_reps as $x=>$x_value) {
            echo 
    $x "-->" $x_value;
            echo 
    "<br>";
            } 
    2-->{2|two|only two}
    a-->{a|some|a very|a definite|a specific|a particluar}
    about-->{about|regarding|concerning|with regards to|in relation to|related to|pertaining to|on the subject of|when it comes to|in regards to|in regard to|with regard to|on the subject off|as regards to}
    access to-->{access to|usage of|use of|the means to access}
    across the-->{across the|around the|all over the|over the|through the|round the|all around the}
    advert-->{advert|advertisement|ad|offer|promotion|special offer|promo|campaign|deal|special promotion|special deal}
    advice-->{advice|guidance|suggestions|tips|assistance|information|help and advice|recommendations|help|aid|instruction|counsel}
    ago-->{ago|back|in the past|earlier|previously|previous|prior}
    agreement-->{agreement|arrangement|contract|deal}


    In order to use preg_replace(),
    I wanted to add some reg_exp so that when I search for

    "a" it does NOT get found in "about" but only as "a"

    ( maybe I should use a \b for word boundary ? )

    Anyway, I changed the FIND array to be:
    $new_words

    "/^2$/"
    "/^a$/"
    "/^about$/"
    "/^access to$/"
    "/^across the$/"
    "/^advert$/"
    "/^advice$/"
    "/^ago$/"
    "/^agreement$/"

    I also change the associated array:
    $new_words_reps:

    "/^2$/"-->{2|two|only two}
    "/^a$/"-->{a|some|a very|a definite|a specific|a particluar}
    "/^about$/"-->{about|regarding|concerning|with regards to|in relation to|related to|pertaining to|on the subject of|when it comes to|in regards to|in regard to|with regard to|on the subject off|as regards to}
    "/^access to$/"-->{access to|usage of|use of|the means to access}
    "/^across the$/"-->{across the|around the|all over the|over the|through the|round the|all around the}
    "/^advert$/"-->{advert|advertisement|ad|offer|promotion|special offer|promo|campaign|deal|special promotion|special deal}
    "/^advice$/"-->{advice|guidance|suggestions|tips|assistance|information|help and advice|recommendations|help|aid|instruction|counsel}
    "/^ago$/"-->{ago|back|in the past|earlier|previously|previous|prior}
    "/^agreement$/"-->{agreement|arrangement|contract|deal}


    OK !

    So now I want to use the preg_replace()

    BUT my question is - do I need to use the associated array
    or do I use the two FIND and REPLACE arrays

    Since these 2 arrays were generated from the same table, they have the same
    natural keys - does that keep them in step when replacing ?

    If I use the two arrays, I guess it should like this ...

    PHP Code:
    $new_data preg_replace($new_words,$replacements,$input_data); 

    If is the assocated array, I don't know how to write that !!

    Am I correct in thinking that the input can be a string ?
    Or does that need to be an array as well ?


    Sorry,
    A lot of questions .. but I am not sure how to do this !!

    Thanks for any help !


    .
    Last edited by jeddik; 11-19-2013 at 08:12 AM.
    Developers Choice Revealed:
    www.devchoice.info
    Which host has won, and why ?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.26428 seconds
  • Memory Usage 4,204KB
  • Queries Executed 14 (?)
More Information
Template Usage (32):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (2)bbcode_code
  • (13)bbcode_php
  • (9)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (22)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (22)postbit
  • (22)postbit_onlinestatus
  • (22)postbit_wrapper
  • (1)showthread_list
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (27):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./includes/functions_threadedmode.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (73):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids_threaded
  • showthread_threaded_construct_link
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • bbcode_parse_start
  • postbit_imicons
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates